2014-09-21 116 views
1

我正在拍摄整个屏幕的屏幕截图,但我想仅拍摄收藏视图的内容的屏幕截图,这是因为上面有一个菜单视图,而且我想只获取集合中的内容,现在我用这个来捕捉屏幕=收藏+菜单:仅获取UICollectionView的屏幕截图?

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

回答

4

此代码捕获yourCollectionView

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

这节省了UIImage的jpg格式,95%的质量在应用程序的文档文件夹中,如果你需要这样做。

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

希望这会帮助你...