Tôi đoán bạn cần chụp một UIView/UIWindow ở độ phân giải võng mạc (960x640 hoặc 2048x1536) Sử dụng UIGraphicsBeginImageContextWithOptions và thiết lập thông số 0.0f quy mô của nó làm cho nó chụp ở độ phân giải tự nhiên (võng mạc cho iPhone 4 và sau hoặc iPad 3).
Mã này chụp UIView của bạn ở độ phân giải gốc
CGRect rect = [captureView bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[captureView.layer renderInContext:context];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Cái này cũng làm như vậy cho toàn màn hình (cửa sổ chính)
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();
Điều này tiết kiệm UIImage ở định dạng jpg với chất lượng 95% trong thư mục tài liệu của ứng dụng
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/capturedImage.jpg"]];
[UIImageJPEGRepresentation(capturedImage, 0.95) writeToFile:imagePath atomically:YES];
bạn có thể thử với điều này cũng UIGraphicsBeginImageContextWithOptions (view.bounds.size, view.opaque, 0.0); –
cảm ơn nhưng không có gì thay đổi! –
Khi bạn nói "chất lượng thấp", ý của bạn là chính xác? Hình ảnh không giống với hình ảnh trên màn hình? Bạn đang sử dụng màn hình hiển thị võng mạc nhưng hình ảnh được chụp không sắc nét? Hình ảnh có bị nhiễu hay bị méo? Điều gì là chính xác sai với nó? – TigerCoding