2012-10-02 33 views
0

我有self.view,但我只想捕獲它的300x320。如何捕獲self.view的特定大小

得到這個代碼:

UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 

UIImage * imgPrintScreen = [[UIImage alloc]init]; 
imgPrintScreen = viewImage; 

什麼我需要在這裏改變爲了做到這一點?

謝謝分配!

回答

2

只需更改要捕獲的大小,而不是使用整個邊界,請使用所需的大小。渲染將從視圖的原點開始,並在超出邊界時剪切。如果您需要更改其中的觀點實際上是剪裁,簡單地啓動圖像內容後翻譯上下文:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.); 
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

如果你的觀點是,例如,600x320和你想的寬度捕捉中間的300點,你會將上下文150點翻譯爲左側:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, -150.f, 0.f); 
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

謝謝你!你幫了我很多! –

相關問題