ios 将UIView保存Image,依据scale

来源:互联网 发布:新开淘宝没销量怎么办 编辑:程序博客网 时间:2024/05/21 23:19

简单而有效的方法:

1、为UIView建立Category

#import <UIKit/UIKit.h>


@interface UIView (saveImageWithScale)


- (UIImage *)saveImageWithScale:(float)scale;


@end


#import "UIView+saveImageWithScale.h"


#import <QuartzCore/QuartzCore.h>


@implementation UIView (saveImageWithScale)


- (UIImage *)saveImageWithScale:(float)scale


{


    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, scale);


    [self.layer renderInContext:UIGraphicsGetCurrentContext()];


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();


    UIGraphicsEndImageContext();


    return image;


}


@end


现在即可将任意的UIView保存为Image并且是真实尺寸。

亲测,有效

参照StackOverFlow

Demo下载
原创粉丝点击