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

前端 未结 3 1670
暗喜
暗喜 2020-12-23 23:33

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

相关标签:
3条回答
  • 2020-12-24 00:15

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-24 00:17

    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;
    }
    
    0 讨论(0)
  • 2020-12-24 00:34
    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.

    0 讨论(0)
提交回复
热议问题