Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone : How to save a view as image ??? (ex.save what you draw )

I found some sample is teach you how to draw on iphone

but it does not say how to save a view as image ?

Does anyone got idea ???

Or any sample will be helpful : )

actually,I'm trying to save user's signature as a image and upload it to server.

Thanks

Webber

like image 956
Webber Lai Avatar asked May 21 '11 02:05

Webber Lai


3 Answers

UIView *view = // your view    
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This gives the image which you can store using –

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[imageData writeToFile:path atomically:YES];

where path is location you want to save to.

like image 160
Deepak Danduprolu Avatar answered Nov 06 '22 07:11

Deepak Danduprolu


In MonoTouch/C# as an extension method:

public static UIImage ToImage(this UIView view) {
    try {
        UIGraphics.BeginImageContext(view.ViewForBaselineLayout.Bounds.Size);
        view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
        return UIGraphics.GetImageFromCurrentImageContext();
    } finally {
        UIGraphics.EndImageContext();
    }
}
like image 29
Herman Schoenfeld Avatar answered Nov 06 '22 05:11

Herman Schoenfeld


Here is a quick method to render any UIView as image. It takes into account the iOS version the device is running on and makes use of the relevant method for acquiring an image representation of a UIView.

More specifically, now there is better method (i.e., drawViewHierarchyInRect:afterScreenUpdates:) for taking screenshot of a UIView on devices running on iOS 7 or later versions, which is, from what i've read, considered to be a more performant way as compared to "renderInContext" method.

More info here: https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW217

USAGE EXAMPLE:

#import <QuartzCore/QuartzCore.h> // don't forget to import this framework in file header.

UIImage* screenshotImage = [self imageFromView:self.view]; //or any view that you want to render as an image.

CODE:

#define IS_OS_7_OR_LATER    ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

- (UIImage*)imageFromView:(UIView*)view {

    CGFloat scale = [UIScreen mainScreen].scale;
    UIImage *image;

    if (IS_OS_7_OR_LATER) {
        //Optimized/fast method for rendering a UIView as image on iOS 7 and later versions.
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, scale);
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    } else {
        //For devices running on earlier iOS versions.
        UIGraphicsBeginImageContextWithOptions(view.bounds.size,YES, scale);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    return image;
}
like image 4
programmer Avatar answered Nov 06 '22 07:11

programmer