Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
46
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

画像をトリミングしてUIImageViewにセットする

CGImageCreateWithImageInRectを使うと、CGImageから任意のCGRectの範囲の画像が作れる。CGImageからUIImageを作るときは、imageWithCGImage:scale:orientation:を使い倍率を設定することで、高解像度のUIImageを作ることができる。

- (void)setImage:(UIImage *)image rect:(CGRect)rect {


    // self は UIImageView

    [self setImage:nil];

    // 画像のクリッピング iPad3 Retina対応
    float scale = [[UIScreen mainScreen] scale];
    CGRect scaledRect = CGRectMake(rect.origin.x * scale,
                                       rect.origin.y * scale,
                                       rect.size.width * scale,
                                       rect.size.height * scale);

    CGImageRef clip = CGImageCreateWithImageInRect(image.CGImage,scaledRect);
    UIImage *clipedImage = [UIImage imageWithCGImage:clip
                                                    scale:scale 
                                              orientation:UIImageOrientationUp];
    [self setImage:clipedImage];
    CGImageRelease(clip);
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
46
Help us understand the problem. What are the problem?