2013-10-07 63 views
2

我正在爲iPhone應用程序編寫圖像編輯器。我使用GPUImage library在畫布上呈現精靈圖像(在GPUImageView上)。我用下面的方法來獲取畫布的截圖:如何獲取包含GPUImageView的視圖的屏幕截圖?

@implementation UIView (SnapshotImage) 
// Return a snapshot image of this view 
- (UIImage*)snapshot{ 

    UIGraphicsBeginImageContextWithOptions(self.bounds.size,YES,0.0f); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // In iOS 7, you can use the following instead of `-renderInContext:`. It should be faster. 
    // [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; 
    [self.layer renderInContext:context]; 

    UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return capturedImage; 
} 
@end 

我用這個方法後得到的關鍵窗口的全屏幕截圖,畫布的領域是空的(沒有任何精靈的形象)。

UIView *keyWindow = [[UIApplication sharedApplication] keyWindow]; 
UIImage *snapshotIamge = [keyWindow snapshot]; 
// Present the image on the view... 

我認爲這是獲取包含GPUImageView(S)的視圖,其使用OpenGLES來渲染圖像不石英的截圖的問題。如何獲取包含GPUImageView的視圖截圖?

當拍攝屏幕截圖時(例如Mac上的cmd + shift + 4),是否可以使用特定的CGRect獲取裁剪後的圖像?

回答

2

如您所見,-renderInContext:不適用於OpenGL ES內容。你需要的是從GPUImage本身提取處理後的圖像。

爲此,請在直接向您的GPUImageView中輸入的過濾器上調用-imageFromCurrentlyProcessedOutput。這將爲您提供一個UIImage,其中包含最終過濾的圖像。然後,您可以直接使用它,也可以將它與周圍視圖中的剩餘內容進行合成。

+0

劑量,這意味着我應該創建並添加新的UIImageView(s)到精靈(s)以呈現從-imageFromCurrentlyProcessedOutput'的UIImage,拍攝快照,然後從精靈中刪除UIImageView(s)相同的運行循環? –