2012-06-11 33 views
11

Làm cách nào để chụp ảnh màn hình 1: 1 của vùng hiển thị UIScrollView? Nội dung có thể lớn hơn hoặc nhỏ hơn giới hạn UIScrollView cũng như ẩn một nửa (Tôi đã triển khai cuộn tùy chỉnh cho nội dung nhỏ hơn, vì vậy nó không nằm ở góc trên cùng bên trái). tôi đã đạt được kết quả mong muốn trên mô phỏng, nhưng không phải trên thiết bị chính nó:Làm cách nào để chụp màn hình của khu vực hiển thị UIScrollView?

-(UIImage *)imageFromCombinedContext:(UIView *)background { 
     UIImage *image; 
     CGRect vis = background.bounds; 
     CGSize size = vis.size; 
     UIGraphicsBeginImageContext(size); 
     [background.layer affineTransform]; 
     [background.layer renderInontext:UIGraphicsGetCurrentContext()]; 
     image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     CGImageRef imref = CGImageCreateWithImageInRect([image CGImage], vis); 
     image = [UIImage imageWithCGImage:imref]; 
     CGImageRelease(imref); 
     return image; 
} 

Trả lời

4

Tôi đã tìm thấy một giải pháp bản thân mình - tôi lấy ảnh chụp màn hình của toàn bộ quan điểm và sau đó cắt nó vào kích thước và vị trí của UIScrollView khung .

-(UIImage *)imageFromCombinedContext:(UIView *)background 
{ 
     UIImage *image; 
     CGSize size = self.view.frame.size; 
     UIGraphicsBeginImageContext(size); 
     [background.layer affineTransform]; 
     [self.view.layer.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     CGImageRef imgRef = CGImageCreateWithImageInRect([image CGImage],background.frame); 
     image = [UIImage imageWithCGImage:imref]; 
     CGImageRelease(imref); 
     return image; 
} 
+0

chấp nhận câu trả lời của bạn sau đó :) – ant

+0

Tôi vẫn phải đợi 20 giờ) – Concuror

+2

Đây không phải là sự thiếu hiệu quả khủng khiếp ient cho quan điểm cuộn lớn? Có gì sai với câu trả lời từ câu trả lời @AbduliamRehmanius? – deanWombourne

29

cách tiếp cận khác sẽ được sử dụng để điều chỉnh contentOffset vùng hiển thị của lớp và chụp chỉ khu vực hiện hữu hình của UIScrollView.

UIScrollView *contentScrollView;....//scrollview instance 

UIGraphicsBeginImageContextWithOptions(contentScrollView.bounds.size, 
             YES, 
             [UIScreen mainScreen].scale); 

//this is the key 
CGPoint offset=contentScrollView.contentOffset; 
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y); 

[contentScrollView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *visibleScrollViewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

Cheers :)

+3

điều này đơn giản hơn rất nhiều và bộ nhớ được tối ưu hóa vì không cần phải cắt ảnh lớn hơn. –

+1

Câu trả lời này là câu trả lời duy nhất có hiệu quả đối với tôi. Cảm ơn. –

+0

Bất cứ ai có may mắn với điều này trên Swift?Tôi có một "Zoomable" scrollview, gọi hàm mở rộng này và nhận được một hình ảnh trống: func ảnh chụp màn hình() -> UIImage { let bù đắp = self.contentOffset UIGraphicsBeginImageContextWithOptions (self.bounds.size, false, UIScreen.mainScreen() .scale) CGContextTranslateCTM (UIGraphicsGetCurrentContext(), -offset.x, -offset.y) self.layer.renderInContext (UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() hình ảnh trở lại } – Ryan

0

câu trả lời @Abduliam Rehmanius của có hiệu suất kém, vì nếu UIScrollView chứa một khu vực nội dung lớn, chúng tôi sẽ rút ra rằng toàn bộ khu vực nội dung (kể cả ngoài giới hạn có thể nhìn thấy). @ Concuror của câu trả lời có vấn đề rằng nó cũng sẽ rút ra bất cứ điều gì đó là trên đầu trang của UIScrollView.

giải pháp của tôi là đưa UIScrollView bên trong một UIView gọi containerView với các giới hạn tương tự và sau đó render containerView:

containerView.renderInContext(context) 
3

Jeffery Sun có câu trả lời đúng. Chỉ cần đặt chế độ xem cuộn của bạn bên trong chế độ xem khác. Nhận chế độ xem vùng chứa để hiển thị trong ngữ cảnh. làm xong.

Trong mã bên dưới, cropView chứa chế độ xem cuộn để được chụp. Giải pháp thực sự đơn giản.

Khi tôi hiểu câu hỏi và lý do tại sao tôi tìm thấy trang này, toàn bộ nội dung của chế độ xem cuộn không muốn - chỉ phần hiển thị.

func captureCrop() -> UIImage { 
    UIGraphicsBeginImageContextWithOptions(self.cropView.frame.size, true, 0.0) 
    self.cropView.layer.renderInContext(UIGraphicsGetCurrentContext()) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image; 
} 
3

Phiên bản Swift của Abduliam Rehmanius answer.

func screenshot() -> UIImage { 

     UIGraphicsBeginImageContextWithOptions(self.scrollCrop.bounds.size, true, UIScreen.mainScreen().scale); 
     //this is the key 
     let offset:CGPoint = self.scrollCrop.contentOffset; 
     CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y); 
     self.scrollCrop.layer.renderInContext(UIGraphicsGetCurrentContext()!); 
     let visibleScrollViewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return visibleScrollViewImage; 
    } 
0

Swift 3.0:

func captureScreen() -> UIImage? { 
    UIGraphicsBeginImageContextWithOptions(self.yourScrollViewName.bounds.size, true, UIScreen.main.scale) 
    let offset:CGPoint = self.yourScrollViewName.contentOffset; 
    UIGraphicsGetCurrentContext()!.translateBy(x: -offset.x, y: -offset.y); 
    self.yourScrollViewName.layer.render(in: UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 
} 

và sử dụng nó như: let Image = captureScreen()

0

Cập nhật nhanh chóng 3+, 4 trên mã @Concuror

func getImage(fromCombinedContext background: UIView) -> UIImage { 
     var image: UIImage? 
     let size: CGSize = view.frame.size 
     UIGraphicsBeginImageContext(size) 
     background.layer.affineTransform() 
     view.layer.render(in: UIGraphicsGetCurrentContext()!) 
     image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     let imgRef = image?.cgImage?.cropping(to: background.frame) 
     image = UIImage(cgImage: imgRef!) 
     // CGImageRelease(imgRef!) // Removing on Swift - 'CGImageRelease' is unavailable: Core Foundation objects are automatically memory managed 
     return image ?? UIImage() 
    } 
Các vấn đề liên quan