全屏图:
-(void)fullScreenshots{
UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图,包括window
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}
截取自定义的大小
iPhone开发应用中抓图程序案例实现是本文要介绍的内容,主要是通过代码来实现抓图程序,具体实现过程,一起来看详细代码。
//获得屏幕图像
- (UIImage *)imageFromView: (UIView *) theView
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
//获得某个范围内的屏幕图像
- (UIImage *)imageFromView: (UIView *) theView atFrame:(CGRect)r
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIRectClip(r);
[theView.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;//[self getImageAreaFromImage:theImage atFrame:r];
}
//保存图片到本地
//保存图片到本地
- (void)saveImageToPhotos:(UIImage*)savedImage
{
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
// 指定回调方法
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
NSString *msg = nil ;
if(error != NULL){
msg = @"保存失败!" ;
}else{
msg = @"保存成功!" ;
}
[MBProgressHUD showError:msg toView:self.view];
}
关于直播间截屏 比较特殊。
目前多数的直播播放是使用openGLES来实现的,使用常用的
UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextFillRect(context, bounds);
CGContextTranslateCTM(context, shrinkSize.width, shrinkSize.height);
[mainWindow.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
不能截取播放器播放的画面,只能截取到一块黑屏
目前使用下面的方法可以直接截取
UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
UIGraphicsBeginImageContextWithOptions(mainWindow.frame.size, NO, 0);
[mainWindow drawViewHierarchyInRect:mainWindow.frame afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
网友评论