LoginSignup
45
46

More than 5 years have passed since last update.

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

Last updated at Posted at 2012-04-27

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);
}
45
46
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
46