2012-01-22 12 views
8

tôi tìm thấy mã này là khá đẹp:Capture UIImage của UIView bao gồm UINavigationBar

+ (UIImage *) imageWithView:(UIView *)view 
{ 
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]); 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return img; 
} 

Nhưng tôi cần phải nắm bắt UINavigationBar cũng vì tôi muốn sử dụng hình ảnh như lớp chuyển tiếp trong UIStoryboardSegue tôi. Bất kỳ ý tưởng?

Trả lời

7

Sử dụng cửa sổ của chế độ xem làm chế độ xem để thanh điều hướng và thanh trạng thái cũng được bao gồm. Nếu bạn cần xóa thanh trạng thái, bạn sẽ phải cắt hình ảnh.

+0

Làm cách nào để tôi chụp màn hình một bộ điều khiển xem bao gồm thanh điều hướng TRƯỚC KHI đẩy nó? Tôi cần phải chụp màn hình màn hình điều khiển xem toàn màn hình đích. – cschuff

+0

Nếu bạn có con trỏ tới bộ điều khiển chế độ xem, bạn có thể dễ dàng truy cập chế độ xem của bộ điều khiển chế độ xem đó ngay cả khi nó chưa được đẩy. –

+0

Có, nhưng 1. thanh điều hướng không thuộc về chế độ xem và 2. tôi có thể truy cập NavigationBar của nó trước khi nó được đẩy hay không. Các thanh điều hướng xuất hiện trong VC2 khác với VC1. – cschuff

2

thay vì view.layer cung cấp tham chiếu về appdelegate.window.layer, nó sẽ hoạt động.

+0

Tính năng này hoạt động với tôi trên iOS 8+ – superpuccio

3

Bạn nên chụp ảnh màn hình của self.navigationController? .view. Bằng cách đó, bạn sẽ có được toàn bộ khung nhìn với navigationBar nhưng không có thanh trạng thái.

if let view = self.navigationController?.view { 
    let snapshot = view.snapshotViewAfterScreenUpdates(false) 
} 
+0

Tính năng này hoạt động trên iOS 8+ – superpuccio

0

Thật ra, không có câu trả lời nào trong số này phù hợp. Đây là cách bạn làm điều đó để có được ảnh chụp màn hình của tất cả mọi thứ bao gồm điều khiển điều hướng

-(void) takeScreenshot 
{ 

    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    { 
     //its iphone 


     CGSize result = [[UIScreen mainScreen] bounds].size; 
     if(result.height == 480 || result.width == 480) 
     { 
      // iPhone Classic 
      screenRect = CGRectMake(0, 0, 320, 480); 
     } 
     if(result.height == 568 || result.width == 568) 
     { 
      // iPhone 5 
      screenRect = CGRectMake(0, 0, 320, 568); 
     } 
    } 
    else 
    { 
     //its pad 
     screenRect = CGRectMake(0, 0, 768, 1024); 
    } 

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

    // This code is for high resolution screenshot 
    UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0); 

    //this was pre iOS 7.0 
    //[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    // iOS 7.x --> 
    [[[[UIApplication sharedApplication] windows] objectAtIndex:0] drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; // Set To YES 

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


    NSData* imageData2 = UIImageJPEGRepresentation(viewImage2, 1.0); 
    NSString* incrementedImgStr2 = [NSString stringWithFormat: @"UserScreenShotPic.jpg"]; 


    // Now we get the full path to the file 
    NSString* fullPathToFile3 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr2]; 
    [imageData2 writeToFile:fullPathToFile3 atomically:NO]; 

}