天天看點

ios 将UIView儲存Image,依據scale

簡單而有效的方法:

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下載下傳