2014-09-21 149 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]; 

希望這會幫助你...