Low quality of capture view context on iPad

前端 未结 1 835
甜味超标
甜味超标 2021-01-01 02:47

I need capture specific UIView , but the result is in low quality how can fix this and increase the quality ?

UIGraphicsBeginImageContext(captureView.bounds.         


        
相关标签:
1条回答
  • 2021-01-01 03:04

    I guess you need a capture a UIView/UIWindow in retina resolution (960x640 or 2048x1536) Using UIGraphicsBeginImageContextWithOptions and set its scale parameter 0.0f makes it capture in native resolution (retina for iPhone 4 and later or iPad 3).

    This code capture your UIView in native resolution

    CGRect rect = [captureView bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [captureView.layer renderInContext:context];   
    UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    This one does the same for full screen (key window)

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CGRect rect = [keyWindow bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyWindow.layer renderInContext:context];   
    UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    This saves the UIImage in jpg format with 95% quality in the app's document folder

    NSString  *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];    
    [UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];
    
    0 讨论(0)
提交回复
热议问题