iOS : Save image with custom resolution

前端 未结 9 2024
长发绾君心
长发绾君心 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:43
    CGSize sizePic = CGSizeMake(320, 460);
        UIGraphicsBeginImageContext(sizePic);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *imagePic = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(imagePic, nil, nil, nil);
    
    0 讨论(0)
  • 2020-12-08 03:47

    have a look at this answer. The code includes rotation but nonetheless the questioner asked the same question: "How to get a […] image from an UIImageView at its full resolution?"

    copied content (in case of deletion or whatever):

    - (UIImage *)capturedView
    {
        float imageScale = sqrtf(powf(self.captureView.transform.a, 2.f) + powf(self.captureView.transform.c, 2.f));    
        CGFloat widthScale = self.captureView.bounds.size.width / self.captureView.image.size.width;
        CGFloat heightScale = self.captureView.bounds.size.height / self.captureView.image.size.height;
        float contentScale = MIN(widthScale, heightScale);
        float effectiveScale = imageScale * contentScale;
    
        CGSize captureSize = CGSizeMake(enclosingView.bounds.size.width / effectiveScale, enclosingView.bounds.size.height / effectiveScale);
    
        NSLog(@"effectiveScale = %0.2f, captureSize = %@", effectiveScale, NSStringFromCGSize(captureSize));
    
        UIGraphicsBeginImageContextWithOptions(captureSize, YES, 0.0);        
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextScaleCTM(context, 1/effectiveScale, 1/effectiveScale);
        [enclosingView.layer renderInContext:context];   
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return img;
    }
    
    0 讨论(0)
  • 2020-12-08 03:49

    This worked for me. I tried with image that i got from Facebook SDK http://pulkitgoyal.in/resizing-high-resolution-images-on-ios-without-memory-issues/

    0 讨论(0)
  • 2020-12-08 03:50

    First get your image in UIImage object. Create your size what ever you want and use following..

    UIImage *image = // you image;
    CGSize size;
    if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
    ([UIScreen mainScreen].scale == 2.0)) {
    
         // RETINA DISPLAY
          size = CGSizeMake(640, 640);
    }
    else {
         // Non Ratina device
          size = CGSizeMake(320, 320);
    }
    
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    

    Now you will get destImage with new resolution.

    Hope this is what you are looking for :)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 03:58

    You can use that :

    UIImageExtras.h

    @interface UIImage (Extras)
    -(UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize;
    @end
    

    UIImageExtras.m

    #import "UIImageExtras.h"
    
    @implementation UIImage (Extras)
    
    - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
    {
    //Image de base
    UIImage *sourceImage = self;
    //Image redimenssionnée
    UIImage *newImage = nil; 
    
    //Taille de l'image de base
    CGSize imageSize = sourceImage.size;
    //Longueur et largeur 
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    
    //Dimension désirée
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    
    //Echelle...
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    //Si taille des image est différentes on redimensionne de facon proportionnelle
    if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
    
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
    
        //Centre l'image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        }
        else if (widthFactor < heightFactor)
            {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
        }       
    
        UIGraphicsBeginImageContext(targetSize);
    
        CGRect thumbnailRect = CGRectZero;
        thumbnailRect.origin = thumbnailPoint;
        thumbnailRect.size.width  = scaledWidth;
        thumbnailRect.size.height = scaledHeight;
    
        [sourceImage drawInRect:thumbnailRect];
    
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        if(newImage == nil) 
            NSLog(@"could not scale image");
    
        UIGraphicsEndImageContext();
    
        return newImage;
    }
    @end
    
    0 讨论(0)
提交回复
热议问题