Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios how to capture a particular portion of screen

I want to capture a particular portion of iPhone screen. I used UIGraphicsBeginImageContextWithOptions, but couldn't capture a portion of screen with this. Please help me.

like image 235
Bai Avatar asked Oct 27 '11 11:10

Bai


4 Answers

you can use this code

UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGRect rect = CGRectMake(250,61 ,410, 255);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 

CGImageRelease(imageRef);
like image 62
Superdev Avatar answered Nov 16 '22 03:11

Superdev


You can take screen shot using UIGraphicsGetImageFromCurrentContext. Wrote below code from memory, so errors possible. Please correct it yourself.

- (UIImage*)captureView:(UIView *)yourView {
    CGRect rect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [yourView.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
like image 24
Krishnabhadra Avatar answered Nov 16 '22 02:11

Krishnabhadra


Here's a swift extension on UIView that will either capture the entire view or a frame within the view. It takes into account the screen scale.

extension UIView {

    func imageSnapshot() -> UIImage {
        return self.imageSnapshotCroppedToFrame(nil)
    }

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage {
        let scaleFactor = UIScreen.mainScreen().scale
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor)
        self.drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
        var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        if let frame = frame {
            // UIImages are measured in points, but CGImages are measured in pixels
            let scaledRect = CGRectApplyAffineTransform(frame, CGAffineTransformMakeScale(scaleFactor, scaleFactor))

            if let imageRef = CGImageCreateWithImageInRect(image.CGImage, scaledRect) {
                image = UIImage(CGImage: imageRef)
            }
        }
        return image
    }
}
like image 25
jDutton Avatar answered Nov 16 '22 01:11

jDutton


the answer by @Superdev is right on spot , what i want to add is a little trick if you want to capture a parent view and avoid subviews ( in particular overlay view ) what you can do is make this subview a property and use the following

CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
_overlayView.hidden = YES;
UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

CGRect rect = CGRectMake(0,0 ,width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);

UIImage *img = [UIImage imageWithCGImage:imageRef];

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);

CGImageRelease(imageRef);

_overlayView.hidden = NO;

its a small trick , hope someone will find it useful

like image 32
Abhinav Singh Avatar answered Nov 16 '22 02:11

Abhinav Singh