2011-10-27 3 views
7

iPhone 화면의 특정 부분을 캡처하고 싶습니다. UIGraphicsBeginImageContextWithOptions을 사용했지만이 부분을 캡처 할 수 없습니다. 도와주세요.ios 화면의 특정 부분을 캡처하는 방법

+0

컴퓨터에서 스크린 샷을 캡처하고 싶거나 코드를 통해 앱 영역을 캡처하고 싶습니까? – mAc

+2

@mAc 분명히 그는 코드로 그것을하고 싶다. 왜 UIGraphicsBeginImageContextWithOptions를 사용해야 할까? – Krishnabhadra

답변

15

UIGraphicsGetImageFromCurrentContext을 사용하여 스크린 샷을 찍을 수 있습니다. 메모리에서 코드 아래에 쓴 것이므로 오류가 발생할 수 있습니다. 직접 수정하십시오.

- (UIImage*)captureView:(UIView *)yourView { 
    CGRect rect = [[UIScreen mainScreen] bounds]; 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [yourView.layer renderInContext:context]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 
25

당신은 명확한 화면을 캡처하는이 코드

UIGraphicsBeginImageContext(self.view.bounds.size); 

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

CGRect rect = CGRectMake(250,61 ,410, 255); 
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect); 

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 

CGImageRelease(imageRef); 
+3

이것은 화면의 특정 영역을 캡쳐해야 할 때 유용하다. – priyanka

+2

굉장한 친구 :) –

+0

나는 이것으로 특별한 부분을 얻었다. 그러나 일부 견해에서는 내용을 숨기고 있습니다. 왜 그런 일이 벌어지고 있는지 알 수 있습니까? – Niharika

0

에게 또 다른 방법을 사용할 수 있습니다;

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
      UIGraphicsBeginImageContextWithOptions(self.captureView.frame.size, NO, [UIScreen mainScreen].scale); 
     else 
      UIGraphicsBeginImageContext(self.captureView.frame.size); 

     [self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
     [viewImage drawInRect:CGRectMake(0.0, 0.0, 640,960)]; 
     //NSData*m_Imgdata=UIImagePNGRepresentation(viewImage); 
     NSData*m_Imgdata=UIImageJPEGRepresentation(viewImage, 1.0); 

     // UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 

     UIGraphicsEndImageContext(); 
1

@Superdev에 의한 대답은 당신이 당신이 할 수있는 것은이 할 수있는 부모 뷰를 캡처하고 (특히 오버레이 뷰에서) 파단을 방지하려면 내가 추가 할 것은 약간의 트릭이며, 정확한 지점에 ITS의 작은 트릭

CGFloat width = CGRectGetWidth(self.view.bounds); 
CGFloat height = CGRectGetHeight(self.view.bounds); 
_overlayView.hidden = YES; 
UIGraphicsBeginImageContext(self.view.bounds.size); 

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

CGRect rect = CGRectMake(0,0 ,width, height); 
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect); 

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 

CGImageRelease(imageRef); 

_overlayView.hidden = NO; 

다음 속성을 하위 뷰 및 사용, 사람이 찾을 희망이 유용

3

여기에 전체보기 또는 뷰 내에서 프레임을 캡처하거나 UIView에 신속한 확장입니다. 화면 크기를 고려합니다.

extension UIView { 

    func imageSnapshot() -> UIImage { 
     return self.imageSnapshotCroppedToFrame(nil) 
    } 

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage { 
     let scaleFactor = UIScreen.mainScreen().scale 
     UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor) 
     self.drawViewHierarchyInRect(bounds, afterScreenUpdates: true) 
     var image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     if let frame = frame { 
      // UIImages are measured in points, but CGImages are measured in pixels 
      let scaledRect = CGRectApplyAffineTransform(frame, CGAffineTransformMakeScale(scaleFactor, scaleFactor)) 

      if let imageRef = CGImageCreateWithImageInRect(image.CGImage, scaledRect) { 
       image = UIImage(CGImage: imageRef) 
      } 
     } 
     return image 
    } 
} 
+1

안녕하세요, jDutton. 나는 너의 대답을 사랑했다. Swift 3.0에 대한 코드를 업데이트하기로 결정했습니다. –

+0

정말 고마워요 !! – Jerome

0

SWIFT 3.0 Xcode8 버전 나를 위해 @jDutton 응답 작업에서 왔습니다. 당신이 충돌하고 다음과 같은 메시지를받을 경우

extension UIView { 
    func imageSnapshot() -> UIImage { 
     return self.imageSnapshotCroppedToFrame(frame: nil) 
    } 

    func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage { 
     let scaleFactor = UIScreen.main.scale 
     UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor) 
     self.drawHierarchy(in: bounds, afterScreenUpdates: true) 
     var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 

     if let frame = frame { 
      // UIImages are measured in points, but CGImages are measured in pixels 
      let scaledRect = frame.applying(CGAffineTransform(scaleX: scaleFactor, y: scaleFactor)) 

      if let imageRef = image.cgImage!.cropping(to: scaledRect) { 
       image = UIImage(cgImage: imageRef) 
      } 
     } 
     return image 
    } 
} 

:

GContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 
CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 

당신의 시간 절약을위한이 solution

희망을 시도!

관련 문제