Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UIImage of a specific area in current screen ios

The following code get UIImage of the current screen:

    UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:ctx];
    UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

If I have a CGRect rect and I want to get only the UIImage of the current screen in that rect, how can I do?

like image 941
thomasdao Avatar asked Jan 23 '13 10:01

thomasdao


1 Answers

For Get Rect (Crop) Image:

UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS You Need);
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

Use following method that return UIImage (as You want size of image)

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }
like image 74
iPatel Avatar answered Nov 17 '22 20:11

iPatel