iOS : Save image with custom resolution

前端 未结 9 2048
长发绾君心
长发绾君心 2020-12-08 03:42

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 im

9条回答
  •  情书的邮戳
    2020-12-08 03:57

    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);
    

    Note that if capture view is not square then the image will be distorted. The saved image will always be square and 1435x1435 pixels.

提交回复
热议问题