Cropping an UIImage

后端 未结 24 2264
轮回少年
轮回少年 2020-11-22 02:45

I\'ve got some code that resizes an image so I can get a scaled chunk of the center of the image - I use this to take a UIImage and return a small, square repre

24条回答
  •  半阙折子戏
    2020-11-22 03:31

    - (UIImage *)getSubImage:(CGRect) rect{
        CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
        CGRect smallBounds = CGRectMake(rect.origin.x, rect.origin.y, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
        UIGraphicsBeginImageContext(smallBounds.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, smallBounds, subImageRef);
        UIImage* smallImg = [UIImage imageWithCGImage:subImageRef];
        UIGraphicsEndImageContext();
    
        return smallImg;
    }
    

提交回复
热议问题