xcode iOS:使用自定义分辨率保存图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11432169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:51:22  来源:igfitidea点击:

iOS : Save image with custom resolution

iphoneobjective-ciosxcodeipad

提问by Mc.Lover

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

but the resolution in iPhone is : 320 x 320 and retina is : 640 x 640

但是 iPhone 的分辨率是:320 x 320,而视网膜是:640 x 640

I would be grateful if you help me to fix this issue .

如果您能帮我解决这个问题,我将不胜感激。

回答by idz

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.

请注意,如果捕获视图不是正方形,则图像将失真。保存的图像将始终为方形和 1435x1435 像素。

回答by Mc.Lover

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?"

看看这个答案。代码包括旋转,但尽管如此,提问者还是问了同样的问题:“如何以全分辨率从 UIImageView 获取 [...] 图像?”

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

回答by Kuldeep

-(UIImage*)processImageRect:(UIImage*)image:(CGSize)sizeToForm {
    // Draw image1  
    UIGraphicsBeginImageContext(CGSizeMake(sizeToForm.width, sizeToForm.height));  
    [image drawInRect:CGRectMake(0.0, 0.0, sizeToForm.width, sizeToForm.height)]; 
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

    return resultingImage;
}

Go with this may solve your issue.

这样做可能会解决您的问题。

回答by Kapil Choubisa

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

首先在 UIImage 对象中获取图像。创建您想要的尺寸并使用以下内容..

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 destImagewith new resolution.

现在您将获得destImage新的分辨率。

Hope this is what you are looking for :)

希望这就是你要找的:)

回答by An-droid

You can use that :

你可以使用它:

UIImageExtras.h

UIImageExtras.h

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

UIImageExtras.m

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

回答by Ravi Kishore

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/

这对我有用。我尝试使用从 Facebook SDK http://pulkitgoyal.in/resizing-high-resolution-images-on-ios-without-memory-issues/获得的图像

回答by chineseGirl

CGSize sizePic = CGSizeMake(320, 460);
    UIGraphicsBeginImageContext(sizePic);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *imagePic = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(imagePic, nil, nil, nil);

回答by Vineet Ravi

#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h> 

+ (UIImage *)resizeImage:(UIImage *)image toResolution:(int)resolution {
NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                       (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                       (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                       (id) kCGImageSourceThumbnailMaxPixelSize : @(resolution)
                                                       };
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
CFRelease(src);
UIImage *img = [[UIImage alloc]initWithCGImage:thumbnail];
return img;

}

}

回答by Mathew Varghese

I think ALAssetRepresentationcan help you.

我认为ALAssetRepresentation可以帮助你。