2011-04-04 55 views
0

開始我使用下面的代碼,使屏幕快照捕捉屏幕的射門從0,0

UIGraphicsBeginImageContext(self.view.frame.size); 

blendMode:kCGBlendModeClear alpha:1.0]; 

[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 

return viewImage; 

它工作正常,但它返回的全屏幕,和我想要一個特定幀的屏幕截圖像
(100,100,200,200),我試圖讓在

UIGraphicsBeginImageContext(self.view.frame.size); 

的變化,但沒有成功

請幫助。

回答

8

試試這個:

float x = 100; 
float y = 100; 
CGSize size = CGSizeMake(200,200); 

UIGraphicsBeginImageContext(size); 
CGContextRef c = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(c, -x, -y); 
[view.layer renderInContext:c]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

+1謝謝你的男人....這對我來說真的是非常非常重要的任務....並且你的技巧奏效了。 – Saawan 2011-04-05 04:02:53

+0

您應該在視網膜顯示設備上開始使用UIGraphicsBeginImageContextWithOptions(size,YES,2.0) – whyoz 2014-09-01 17:41:19

1

只使用三個行代碼。

UIGraphicsBeginImageContext(CGSizeMake(320, 480)); 
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *frame= UIGraphicsGetImageFromCurrentImageContext();