iPhone How-to:给UIView拍照
基本原理就是主要将UIView的layer描绘到图形上下文。UIView全局拍照和局域拍照的代码如下: 1 UIView全局拍照 - (UIImage *) screenImage:(UIView *)view {
UIImage *screenImage; UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
} 2 UIView局域拍照 - (UIImage *) screenImage:(UIView *)view rect:(CGRect)rect {
CGPoint pt = rect.origin;
UIImage *screenImage; UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context,
CGAffineTransformMakeTranslation(-(int)pt.x, -(int)pt.y));
[view.layer renderInContext:context];
screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
}
UIImage *screenImage; UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
} 2 UIView局域拍照 - (UIImage *) screenImage:(UIView *)view rect:(CGRect)rect {
CGPoint pt = rect.origin;
UIImage *screenImage; UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context,
CGAffineTransformMakeTranslation(-(int)pt.x, -(int)pt.y));
[view.layer renderInContext:context];
screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。