问题描述
您好我尝试捕获视图然后将其另存为照片库,但我需要为捕获的图像创建自定义分辨率,这是我的代码,但是当应用程序保存图像时分辨率很低!
Hi I am try to capture a view then save as an image into Photo Library , but I need create a custom resolution for captured image , here is my code but when app saves the images the resolution is low !
UIGraphicsBeginImageContextWithOptions(self.captureView.bounds.size, self.captureView.opaque, 0.0);
[self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
CGRect cropRect = CGRectMake(0 ,0 ,1435 ,1435);
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], cropRect);
CGImageRelease(imageRef);
UIImageWriteToSavedPhotosAlbum(screenshot , nil, nil, nil);
UIGraphicsEndImageContext();
但iPhone的分辨率为:320 x 320,视网膜为:640 x 640
but the resolution in iPhone is : 320 x 320 and retina is : 640 x 640
如果您帮助我解决此问题,我将不胜感激。
I would be grateful if you help me to fix this issue .
推荐答案
您的代码非常接近。您需要做的是以自定义分辨率重新渲染屏幕截图。我修改了你的代码:
Your code is pretty close. What you need to do is re-render the screenshot at the custom resolution. I modified your code to do this:
UIView* captureView = self.view;
/* Capture the screen shoot at native resolution */
UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, captureView.opaque, 0.0);
[captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* Render the screen shot at custom resolution */
CGRect cropRect = CGRectMake(0 ,0 ,1435 ,1435);
UIGraphicsBeginImageContextWithOptions(cropRect.size, captureView.opaque, 1.0f);
[screenshot drawInRect:cropRect];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
/* Save to the photo album */
UIImageWriteToSavedPhotosAlbum(customScreenShot , nil, nil, nil);
请注意,如果捕获视图不是方形,则图像将会失真。保存的图像将始终为方形和1435x1435像素。
Note that if capture view is not square then the image will be distorted. The saved image will always be square and 1435x1435 pixels.
这篇关于iOS:使用自定义分辨率保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!