只获取UICollectionView的屏幕截图?

时间:2014-09-21 08:23:27

标签: ios objective-c

我正在拍摄整个屏幕的屏幕截图,但我想拍一张只有收藏视图内容的屏幕截图,这是因为上面有一个菜单视图,我想只得到集合中的内容,现在我用它来捕获screen = collection + menu:

 UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
                       CGRect trect = rect;
                       UIGraphicsBeginImageContext(trect.size);
                       CGContextRef context = UIGraphicsGetCurrentContext();
                       [keyWindow.layer renderInContext:context];
                       UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
                       UIGraphicsEndImageContext();

1 个答案:

答案 0 :(得分:4)

此代码捕获yourCollectionView

CGRect rect = [yourCollectionView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[yourCollectionView.layer renderInContext:context];   
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

如果您需要,可以在应用程序的文档文件夹中以99%的质量保存Upg图像。

NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];    
[UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];

希望这会对你有帮助......