2011-10-27 39 views
7

Tôi muốn chụp một phần cụ thể của màn hình iPhone. Tôi đã sử dụng UIGraphicsBeginImageContextWithOptions, nhưng không thể chụp một phần màn hình với điều này. Xin hãy giúp tôi.ios cách chụp một phần cụ thể của màn hình

+0

bạn muốn chụp ảnh màn hình từ máy của mình hoặc bạn muốn chụp một khu vực trong ứng dụng của mình thông qua mã? – mAc

+2

@mAc rõ ràng là anh ấy muốn làm điều đó với mã..không phải lý do tại sao anh ấy sử dụng UIGraphicsBeginImageContextWithOptions – Krishnabhadra

Trả lời

15

Bạn có thể chụp màn hình bằng cách sử dụng UIGraphicsGetImageFromCurrentContext. Đã viết mã bên dưới từ bộ nhớ, do đó, có thể xảy ra lỗi. Vui lòng tự sửa nó.

- (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

bạn có thể sử dụng mã này

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

điều này hữu ích khi bạn phải nắm bắt khu vực cụ thể của màn hình. – priyanka

+2

bạn thân tuyệt vời :) –

+0

Tôi nhận được phần đặc biệt này. Nhưng trong một số quan điểm, nó đang ẩn nội dung của nó. Bạn có thể cho một số ý tưởng tại sao nó xảy ra? – Niharika

0

phương pháp khác để chụp ảnh màn hình rõ ràng;

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

câu trả lời bằng cách @Superdev là đúng tại chỗ, những gì tôi muốn thêm là một mẹo nhỏ nếu bạn muốn chụp một cái nhìn mẹ và tránh subviews (theo quan điểm của lớp phủ đặc biệt) những gì bạn có thể làm là làm cho điều này subview một tài sản và sử dụng như sau

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; 

nó một thủ thuật nhỏ, hy vọng ai đó sẽ tìm thấy nó hữu ích

3

Dưới đây là một phần mở rộng nhanh chóng trên UIView mà một trong hai sẽ nắm bắt được toàn bộ quan điểm hoặc một khung trong tầm nhìn. Nó đưa vào tài khoản quy mô màn hình.

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

Xin chào, jDutton. Tôi yêu câu trả lời của bạn. Quyết định cập nhật mã của bạn cho Swift 3.0. –

+0

Cảm ơn bạn rất nhiều !! – Jerome

0

SWIFT 3.0 Xcode8 Version đến từ @jDutton câu trả lời và làm việc cho tôi.

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 
    } 
} 

nếu bạn sụp đổ và nhận được thông báo sau:

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. 

thử solution

Hy vọng điều này cho tiết kiệm thời gian của bạn!

Các vấn đề liên quan