截图功能的实现
简单实现了最基本的截图功能
/** 获取屏幕截图 */
+(UIImage *) getCurrentScreenShot;
/** 获取某个view 上的截图 */
+(UIImage *) getCurrentViewShot:(UIView *) view;
/** 获取某个scrollview 上的截图 */
+(UIImage *) getCurrentScrollviewShot:(UIScrollView *) scrollview;
/** 获取某个 范围内的 截图 */
+ (UIImage *) getCurrentInnerViewShot: (UIView *) innerView atFrame:(CGRect)rect;
实现方法如下所示
#pragma mark 获取当前屏幕的截图
+(UIImage *) getCurrentScreenShot
{
CGSize size = [UIScreen mainScreen].bounds.size;
CGFloat scale = [UIScreen mainScreen].scale;
UIGraphicsBeginImageContextWithOptions(size, YES, scale);
[[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+(UIImage *) getCurrentViewShot:(UIView *) view
{
CGSize size = view.frame.size;
CGFloat scale = [UIScreen mainScreen].scale;
UIGraphicsBeginImageContextWithOptions(size, YES, scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+(UIImage *) getCurrentScrollviewShot:(UIScrollView *) scrollview
{
CGSize size = scrollview.contentSize;
CGFloat scale = [UIScreen mainScreen].scale;
UIGraphicsBeginImageContextWithOptions(size, YES, scale);
//获取当前scrollview的frame 和 contentOffset
CGRect saveFrame = scrollview.frame;
CGPoint saveOffset = scrollview.contentOffset;
//置为起点
scrollview.contentOffset = CGPointZero;
scrollview.frame = CGRectMake(0, 0, scrollview.contentSize.width, scrollview.contentSize.height);
[scrollview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//还原
scrollview.frame = saveFrame;
scrollview.contentOffset = saveOffset;
return image;
}
//获得某个范围内的屏幕图像
+ (UIImage *) getCurrentInnerViewShot: (UIView *) innerView atFrame:(CGRect)rect
{
UIGraphicsBeginImageContext(innerView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIRectClip(rect);
[innerView.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
如遇到对于图片大小有要求的时候,我们需要对图片进行压缩
压缩图片到指定大小
-(UIImage *)scaleImage:(UIImage *)image toKb:(NSInteger)kb{
if (!image) {
return image;
}
if (kb<1) {
return image;
}
kb*=1024;
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
NSData *imageData = UIImageJPEGRepresentation(image, compression);
while ([imageData length] > kb && compression > maxCompression) {
compression -= 0.1;
imageData = UIImageJPEGRepresentation(image, compression);
}
NSLog(@"当前大小:%fkb",(float)[imageData length]/1024.0f);
UIImage *compressedImage = [UIImage imageWithData:imageData];
return compressedImage;
}
网友评论