1
2
3
4
5
6
7
8
|
-(UIImage*)convertViewToImage:(UIView*)v{ CGSize
s = v.bounds.size; UIGraphicsBeginImageContext(s); [v.layer
renderInContext:UIGraphicsGetCurrentContext()]; UIImage*image
= UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } |
因此推測,这种方法适用于iPhone4之前的机型。iPhone4后,因为採用了Retain高清屏,在转换时须要依据屏幕密度做个处理,又到到以下的答案:
1
2
3
4
5
6
7
8
|
-(UIImage*)convertViewToImage:(UIView*)v{ CGSize
s = v.bounds.size; UIGraphicsBeginImageContextWithOptions(s, NO ,
v.layer.contentsScale); [v.layer
renderInContext:UIGraphicsGetCurrentContext()]; UIImage*image
= UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } |
1
2
3
4
5
6
7
8
9
|
-(UIImage*)convertViewToImage:(UIView*)v{ CGSize
s = v.bounds.size; //
以下方法,第一个參数表示区域大小。第二个參数表示是否是非透明的。
UIGraphicsBeginImageContextWithOptions(s, NO ,
[UIScreen mainScreen].scale); [v.layer
renderInContext:UIGraphicsGetCurrentContext()]; UIImage*image
= UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } |
原文:http://www.cnblogs.com/slgkaifa/p/6741745.html