2013-06-17 46 views
16

Đầu tiên của tôi Thêm UILable trên UIImageView và sau đó là ảnh chụp màn hình UIView, hình ảnh không đúng chụp UIView Khung tôi cũng đính kèm url hình ảnh.Chụp UIView và Lưu dưới dạng Hình ảnh

enter image description here

1) Original Image Link :-

enter image description here

2) After Capture the image Link :-

Code: -

UIGraphicsBeginImageContext(viewImage.frame.size); 
    [viewImage.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *vwImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    NSData *data=UIImagePNGRepresentation(vwImage); 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    // NSString *imgName = [NSString stringWithFormat:imagename]; 
    NSString *strPath = [documentsDirectory stringByAppendingPathComponent:imagename]; 
    [data writeToFile:strPath atomically:YES]; 
+0

Tôi nghĩ rằng ảnh chụp màn hình đã được chụp một cách chính xác. Vấn đề tôi thấy ở phía bên kia. Bạn có chắc chắn rằng UIView của bạn có cùng kích thước với hình ảnh nguồn không? Nếu không, thì bạn có thêm UIImage vào chế độ xem của mình mà không thay đổi kích thước của nó không? Bạn có chắc không? :) 'Couse nếu bạn có một hình ảnh có kích thước 200x500, nhưng bạn đã vẽ nó trên UIView kích thước 300x700 cho phép UIView kéo dài hình ảnh cho nó chiếm tất cả không gian, sau đó bạn sẽ có ảnh chụp màn hình kích thước UIView, chứ không phải nguồn hình ảnh – makaron

+0

khi tôi lưu hình ảnh chụp trong thư viện ảnh tại thời điểm đó ngày trong màn hình không thích hợp – Rushabh

+0

Ah, ok, đã nhận nó ngay bây giờ. Hãy thử để kiểm tra câu trả lời của tôi ở đây: http://stackoverflow.com/questions/16062974/place-an-image-on-top-of-another-image/16064804#16064804 Có một mã hoàn toàn làm việc cho mục đích của bạn – makaron

Trả lời

21

Bạn có thể lấy ảnh chụp màn hình của bất kỳ UIView hoặc chụp bất kỳ UIView và lưu nó dưới dạng hình ảnh có mã bên dưới.

- (UIImage *)captureView { 

    //hide controls if needed 
    CGRect rect = [self.view bounds]; 

    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [self.view.layer renderInContext:context]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return img; 

} 
+0

khi tôi lưu hình ảnh chụp đó trong thư viện ảnh tại thời điểm đó trong ngày không hiển thị thích hợp – Rushabh

+0

bạn thử nó ra phương pháp này ?? và cũng thay đổi Autoresize tài sản của UILable –

+0

có tôi thử nó :) – Rushabh

0

Hãy thử như thế này,

UIGraphicsBeginImageContext(viewImage.frame.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[[UIColor blackColor] set]; 
CGContextFillRect(ctx, viewImage.frame);  

[viewImage.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *vwImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

NSData *data=UIImagePNGRepresentation(vwImage); 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
// NSString *imgName = [NSString stringWithFormat:imagename]; 
NSString *strPath = [documentsDirectory stringByAppendingPathComponent:imagename]; 
[data writeToFile:strPath atomically:YES]; 
2

bạn có thể sử dụng mã dưới đây để

UIGraphicsBeginImageContext(pictureView.bounds.size); 
      

[pictureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
13

Take snapshot của một UIView

vượt qua cái nhìn của bạn để chức năng này nó sẽ tự xử lý mọi thứ (Không n eed để thiết lập giới hạn hoặc khung)

- (UIImage *)takeSnapshotOfView:(UIView *)view 
{ 
    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width, view.frame.size.height)); 
    [view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height) afterScreenUpdates:YES]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 
+0

[view.layer renderInContext: ctx] giải pháp được đề xuất ở khắp mọi nơi trên internet là ném CGGetColorAlpha EXC_BAD_ACCESS, nhưng điều này làm việc như một sự quyến rũ. –

7

Swift 3 Version:

func takeSnapshotOfView(view:UIView) -> UIImage? { 
     UIGraphicsBeginImageContext(CGSize(width: view.frame.size.width, height: view.frame.size.height)) 
     view.drawHierarchy(in: CGRect(x: 0.0, y: 0.0, width: view.frame.size.width, height: view.frame.size.height), afterScreenUpdates: true) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
Các vấn đề liên quan