Mike Simz
Mike Simz

Reputation: 4036

How to make UIImage of specific CGRect in view

I have a full screen image displayed and what I would like to do is be able to provide a CGRect and using that rect get the area (or the rect) of the full screen image that is within it and create a UIImage of it. How can I accomplish this? I've been racking my brain over this one so any help would be greatly appreciated! Thanks in advance

Upvotes: 1

Views: 495

Answers (1)

Kumar Utsav
Kumar Utsav

Reputation: 2841

Try this code !

UIImage* crop(UIImage *image, rect) {
    UIGraphicsBeginImageContextWithOptions(rect.size, false, [image scale]);
    [image drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)];
    cropped_image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return cropped_image;
}

Upvotes: 1

Related Questions