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 up开发者_JS百科load it to server.
Thanks
Webber
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.
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;
}
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();
}
}
精彩评论