Cropping an UIImage

后端 未结 24 1808
轮回少年
轮回少年 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:46

    Update 2014-05-28: I wrote this when iOS 3 or so was the hot new thing, I'm certain there are better ways to do this by now, possibly built-in. As many people have mentioned, this method doesn't take rotation into account; read some additional answers and spread some upvote love around to keep the responses to this question helpful for everyone.

    Original response:

    I'm going to copy/paste my response to the same question elsewhere:

    There isn't a simple class method to do this, but there is a function that you can use to get the desired results: CGImageCreateWithImageInRect(CGImageRef, CGRect) will help you out.

    Here's a short example using it:

    CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
    // or use the UIImage wherever you like
    [UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
    CGImageRelease(imageRef);
    

提交回复
热议问题