พระเอกของการ crop รูปก็คือ CGImageCreateWithImageInRect วิธีการใช้งาน function นี้ให้ใส่ parameter เพียงสองตัวเท่านั้น คือ
เพียงเท่านี้ เราก็จะได้รูปที่ crop แล้ว
รูปด้านบนคือรูปต้นฉบับ รูปด้านล่างคือรูปที่ถูก crop แล้ว
ที่มา: How to Crop an Image
Related Link from Roti
- UIImage คือ รูปที่เราต้องการ crop
- CGRect คือ ตำแหน่งที่เราต้องการ crop
UIImage *image = [UIImage imageNamed: @"witch.png"];
CGRect cropRect = CGRectMake(0, 0, 160, 115);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *newImage = [UIImage imageWithCGImage: imageRef];
UIImageView *newImageView = [[UIImageView alloc] initWithImage: newImage];
[window addSubview: newImageView];
รูปด้านบนคือรูปต้นฉบับ รูปด้านล่างคือรูปที่ถูก crop แล้ว
ที่มา: How to Crop an Image
Related Link from Roti

0 comments:
Post a Comment