UIImageJPEGRepresentation在视网膜显示器上提供2幅图像 [英] UIImageJPEGRepresentation giving 2x images on retina display

查看:33
本文介绍了UIImageJPEGRepresentation在视网膜显示器上提供2幅图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,它创建了一个图像,然后向其添加一些效果并缩小其尺寸以制作 largeThumbnail .

I have this code, which creates an image, and then adds some effects to it and sizes it down to make largeThumbnail.

UIImage *originalImage = [UIImage imageWithData:self.originalImage];
thumbnail = createLargeThumbnailFromImage(originalImage);

NSLog(@"thumbnail: %f", thumbnail.size.height);
NSData *thumbnailData = UIImageJPEGRepresentation(thumbnail, 1.0);

稍后:

UIImage *image = [UIImage imageWithData:self.largeThumbnail];
NSLog(@"thumbnail 2: %f", image.size.height);

NSLog返回:

thumbnail: 289.000000
thumbnail 2: 578.000000

如您所见,当它将图像从数据转换回时,它的大小是原来的2倍.为什么会发生这种情况的任何想法?

As you can see, when it converts the image back from data, it makes it 2x the size. Any ideas why this may be happening?

大缩略图:

UIImage *createLargeThumbnailFromImage(UIImage *image) {
    UIImage *resizedImage;

        resizedImage = [image imageScaledToFitSize:LARGE_THUMBNAIL_SIZE];

    CGRect largeThumbnailRect = CGRectMake(0, 0, resizedImage.size.width, resizedImage.size.height);

    UIGraphicsBeginImageContextWithOptions(resizedImage.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    //Image
    CGContextTranslateCTM(context, 0, resizedImage.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, largeThumbnailRect, resizedImage.CGImage);

    //Border
    CGContextSaveGState(context);
    CGRect innerRect = rectForRectWithInset(largeThumbnailRect, 1.5);
    CGMutablePathRef borderPath = createRoundedRectForRect(innerRect, 0);
    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextSetLineWidth(context, 3);
    CGContextAddPath(context, borderPath);
    CGContextStrokePath(context);
    CGContextRestoreGState(context);

    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return thumbnail;
}

推荐答案

尝试替换加载第二张图片的部分:

Try replacing the part where you load the second image:

UIImage *image = [UIImage imageWithData:self.largeThumbnail];

与此:

UIImage *jpegImage = [UIImage imageWithData:self.largeThumbnail];
UIImage *image = [UIImage imageWithCGImage:jpegImage.CGImage scale:originalImage.scale orientation:jpegImage.imageOrientation];

这里发生的是未设置图像比例,因此您获得了两倍的图像尺寸.

What happens here is that the image scale is not set, so you get double image dimensions.

这篇关于UIImageJPEGRepresentation在视网膜显示器上提供2幅图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆