2012-10-02 3 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

감사합니다! 당신이 날 많이 도와 줬어! –

관련 문제