iOS : Save image with custom resolution

前端 未结 9 2042
长发绾君心
长发绾君心 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: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;
    }
    

提交回复
热议问题