2011-09-15 43 views
7

Tôi đang cố gắng chụp (ảnh chụp màn hình) chế độ xem. Đối với điều đó tôi đang sử dụng một đoạn mã được hiển thị bên dưới để lưu nó vào thư mục tài liệu của tôi dưới dạng hình ảnh PNG.Chụp màn hình

UIGraphicsBeginImageContextWithOptions(highlightViewController.fhView.centerView.frame.size, YES, 1.0); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"1.png"]; 
NSData *imageData = UIImagePNGRepresentation(screenshot); 
[imageData writeToFile:appFile atomically:YES]; 
UIGraphicsEndImageContext(); 

Câu hỏi: tôi có thể chụp một phần chế độ xem không? Bởi vì trong đoạn code trên tôi không thể thay đổi nguồn gốc (frame). Nếu bất cứ ai có cách tiếp cận khác để nắm bắt một phần cụ thể của xem xin vui lòng chia sẻ nó.

+0

Bạn muốn chụp từ chỉ mã? Hoặc một số phím tắt có thể làm việc cho bạn? – Nitish

+0

Cảm ơn bạn đã phát lại, tôi chỉ muốn sử dụng mã. – ajay

Trả lời

6

Bạn có thể cắt hình ảnh: http://iosdevelopertips.com/graphics/how-to-crop-an-image.html

CGRect rect = CGRectMake(0,0,10,10); 
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], rect); 
UIImage *croppedScreenshot = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 
+2

+1 cảm ơn vì sự giúp đỡ của bạn – ajay

+1

Tôi rất vui vì tôi có thể giúp bạn! –

0

Xem nếu bạn có thể chỉ định rect như thế này và sau đó đi chụp màn hình.

CGRect requiredRect = CGRectMake(urView.frame.origin.x, urView.frame.origin.y, urView.bounds.size.width, urView.bounds.size.height); 
UIGraphicsBeginImageContext(requiredRect.size); 

Bạn có thể thay đổi nguồn gốc và xem nó có hoạt động hay không. Nếu cách này không hiệu quả, bạn có thể thử xén hình ảnh như được đề cập bởi @mcb

+0

Xin chào, là câu hỏi về cắt xén hoặc chụp. Tôi nghĩ rằng đó là về chụp hình mặc dù. Có thể là nhận thức của bạn khác với tôi. Nhưng tốt trả lời anyways :) –

+0

@ Parth Bhatt: Thực sự cắt xén là một lựa chọn khác. Đó là tất cả. – visakh7

+0

Cảm ơn câu trả lời của bạn, tôi muốn chụp màn hình trên màn hình trong một phần cụ thể. – ajay

1

Hãy thử mã này. Điều này chắc chắn hoạt động như tôi đã triển khai trong nhiều dự án của tôi:

- (UIImage *)image 
{ 
    if (cachedImage == nil) { 
     //YOU CAN CHANGE THE FRAME HERE TO WHATEVER YOU WANT TO CAPTURE 
     CGRect imageFrame = CGRectMake(0, 0, 400, 300); 
     UIView *imageView = [[UIView alloc] initWithFrame:imageFrame]; 
     [imageView setOpaque:YES]; 
     [imageView setUserInteractionEnabled:NO]; 

     [self renderInView:imageView withTheme:nil];   

     UIGraphicsBeginImageContext(imageView.bounds.size); 
      CGContextRef c = UIGraphicsGetCurrentContext(); 
      CGContextGetCTM(c); 
      CGContextScaleCTM(c, 1, -1); 
      CGContextTranslateCTM(c, 0, -imageView.bounds.size.height); 
      [imageView.layer renderInContext:c]; 
      cachedImage = [UIGraphicsGetImageFromCurrentImageContext() retain]; 

      // rescale graph 
      UIImage* bigImage = UIGraphicsGetImageFromCurrentImageContext(); 
      CGImageRef scaledImage = [self newCGImageFromImage:[bigImage CGImage] scaledToSize:CGSizeMake(100.0f, 75.0f)]; 
      cachedImage = [[UIImage imageWithCGImage:scaledImage] retain]; 
      CGImageRelease(scaledImage); 
     UIGraphicsEndImageContext(); 

     [imageView release]; 
    } 

    return cachedImage; 
} 

Tôi hy vọng điều này sẽ giúp bạn.

0

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; 
rect = CGRectMake(250,61 ,410, 255); 
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect); 

UIImage *img = [UIImage imageWithCGImage:imageRef]; 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 
CGImageRelease(imageRef); 
Các vấn đề liên quan