如何获得与原始iOS一样高质量的图像?

时间:2017-08-21 08:46:21

标签: ios objective-c uiimage uiimagepngrepresentation

我已经审核并应用了许多参考调整大小相同的图像质量,但无法获得。

我可以从UIView获取图像并使用下面提到的代码调整大小: -

- (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData* imageData =  UIImagePNGRepresentation(image);
    UIImage* pngImage = [UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil); // PNG image size: {750, 1334}
    NSLog(@"PNG Image Size: %@", pngImage);


    CGSize size = CGSizeMake(375, 667);
    [self imageWithImage1:pngImage scaledToSize:size];
    [self imageWithImage2:pngImage scaledToSize:size];

    return image;
}

- (UIImage *)imageWithImage1:(UIImage *)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData* imageData1 =  UIImagePNGRepresentation(newImage);
    UIImage* scaledImage1 = [UIImage imageWithData:imageData1];
    UIImageWriteToSavedPhotosAlbum(scaledImage1, nil, nil, nil); // PNG Resized image 1 size: {375, 667}
    NSLog(@"PNG Scaled Image Size: %@", scaledImage1);

    return newImage;
}

-(UIImage *)imageWithImage2:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a bitmap context.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 1);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData* imageData1 =  UIImagePNGRepresentation(newImage);
    UIImage* scaledImage1 = [UIImage imageWithData:imageData1];
    UIImageWriteToSavedPhotosAlbum(scaledImage1, nil, nil, nil); // PNG Resized image 2 size: {375, 667}
    NSLog(@"PNG Scaled Image Size: %@", scaledImage1);

    return newImage;
}

PNG图片尺寸:{750,1334}

调整大小后我从PNG图像得到的图像都是{375,667}。根据我的要求,这是正确的。

但是,调整后的图像质量均低于原始PNG图像。我可以认识到按钮,图标和文字模糊不清。

我该如何解决?请提前帮助,谢谢。

我需要调整大小的图像,其质量与PNG图像相同。

PNG图片:

enter image description here

PNG已调整图片1:

enter image description here

PNG已调整图片2:

enter image description here

1 个答案:

答案 0 :(得分:0)

以高质量调整大小

 - (UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
    {
        // Create a bitmap context.
        UIGraphicsBeginImageContextWithOptions(newSize, YES, image.scale);
        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;

    }