2010-08-31 31 views
12

Có thể trong mục tiêu C mà chúng ta có thể chụp màn hình của màn hình và lưu trữ hình ảnh này trong UIImage.cách chụp ảnh màn hình của iPhone theo cách lập trình?

+1

thể trùng lặp http://stackoverflow.com/questions/2214957/how-do-i-take-a-screen-shot-of-a-uiview –

+1

thể trùng lặp của [Làm thế nào để có một ảnh chụp màn hình lập trình ] (http://stackoverflow.com/questions/2200736/how-to-take-a-screenshot-programmatically) –

Trả lời

15

Bạn cần phải tạo ra một bối cảnh bitmap kích thước của màn hình của bạn và sử dụng

[self.view.layer renderInContext:c] 

để sao chép tầm nhìn của bạn trong đó. Khi việc này hoàn tất, bạn có thể sử dụng

CGBitmapContextCreateImage(c) 

để tạo CGImage từ ngữ cảnh của bạn.

Lập:

CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size; 
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, screenSize.height, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast); 
CGContextTranslateCTM(ctx, 0.0, screenSize.height); 
CGContextScaleCTM(ctx, 1.0, -1.0); 

[(CALayer*)self.view.layer renderInContext:ctx]; 

CGImageRef cgImage = CGBitmapContextCreateImage(ctx); 
UIImage *image = [UIImage imageWithCGImage:cgImage]; 
CGImageRelease(cgImage); 
CGContextRelease(ctx); 
[UIImageJPEGRepresentation(image, 1.0) writeToFile:@"screen.jpg" atomically:NO]; 

Lưu ý rằng nếu bạn chạy mã của bạn để đáp ứng với một nhấp chuột vào một UIButton, hình ảnh của bạn sẽ cho thấy nút nhấn.

+0

Bạn có thể xây dựng ở đây không? Thanx – user12345

+0

Chỉ muốn đề cập rằng bạn đã bỏ lỡ việc phát hành colorSpaceRef –

+1

Ngoài ra, 'self' là gì? Điều gì về biến đổi cửa sổ? Bạn có biết điều gì sẽ xảy ra khi bàn phím/cảnh báo được hiển thị không? – Sulthan

22

Mã trước giả định rằng chế độ xem được ghi lại trên màn hình chính ... có thể không.

Công việc này có luôn luôn ghi lại nội dung của cửa sổ chính không? (Cảnh báo: biên soạn trong StackOverflow)


- (UIImage *) captureScreen { 
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; 
    CGRect rect = [keyWindow bounds]; 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [keyWindow.layer renderInContext:context]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return img; 
} 
+0

Tôi tạo hình ảnh động trong chế độ xem nhưng mã này bỏ qua hoạt ảnh (không bị xóa sau khi hoàn thành) – Tibidabo

+0

Cảm ơn bạn! Đây là phương pháp dễ nhất mà tôi đã tìm thấy cho đến nay. – Greg

+0

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

2

thử này ...

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

- (void)saveScreenshotToPhotosAlbum:(UIView *)view 
{ 
    UIImageWriteToSavedPhotosAlbum([self captureView:self.view], nil, nil,nil); 
} 
0

Sử dụng mã này để chụp ảnh màn hình:

-(void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
UIGraphicsBeginImageContext(self.webView.bounds.size); 

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

    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    UIImageWriteToSavedPhotosAlbum(screenshotImage, nil, nil, nil); 

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 


    NSString *str1=[NSString stringWithFormat:@"%d",myInt]; 

    NSString *pngFilePath = [NSString stringWithFormat:@"%@/page_%@.png",docDir,str1]; // any name u want for image 
    NSLog(@"%@",pngFilePath); 
    NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(screenshotImage)]; 
    [data1 writeToFile:pngFilePath atomically:YES]; 
} 
2
UIGraphicsBeginImageContext(self.window.bounds.size); 
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
NSData * data = UIImagePNGRepresentation(image); 
[data writeToFile:@"foo.png" atomically:YES]; 

CẬP NHẬT tháng 4 năm 2011: để hiển thị võng mạc, thay đổi dòng đầu tiên vào này:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
{ 
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale); 
} 
else 
{ 
    UIGraphicsBeginImageContext(self.window.bounds.size); 
} 
0
- (void)SnapShot { 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale); 
    } else { 
     UIGraphicsBeginImageContext(self.view.bounds.size); 
    } 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    NSData *data = UIImagePNGRepresentation(image); 
    [data writeToFile:@"snapshot.png" options:NSDataWritingWithoutOverwriting error:Nil]; 
    [data writeToFile:@"snapshot.png" atomically:YES]; 

    UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:data], nil, nil, nil); 
} 
+1

Đó là 100% làm việc trong chương trình của tôi, tại sao bỏ phiếu xuống.?! –

1

// 100% làm việc

- (UIImage *)screenshot 
{     
    UIGraphicsBeginImageContextWithOptions(self.main_uiview.bounds.size, NO, 2.0f); 
    [self.main_uiview drawViewHierarchyInRect:_main_uiview.bounds afterScreenUpdates:YES]; 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 
0

Câu trả lời của @dhaya đang làm việc cho tôi, nhưng đừng quên cập nhật cài đặt "Info.plist" để lưu hình ảnh trên thư viện. Mã này cần phải được thêm vào tệp "Info.plist" để lưu hình ảnh chụp trên ảnh libraray.

<key>NSPhotoLibraryAddUsageDescription</key> 
<string>NeedLibrary access for save Images on Album</string> 
Các vấn đề liên quan