iphone - UIImageJPEGRepresentation 在视网膜显示屏上提供 2x 图像

标签 iphone objective-c ios

我有这段代码,它创建一个图像,然后向它添加一些效果并将其缩小以制作 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 倍。知道为什么会发生这种情况吗?

大缩略图代码:

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

最佳答案

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

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

用这个:

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

这里发生的是图像比例没有设置,所以你得到双倍的图像尺寸。

关于iphone - UIImageJPEGRepresentation 在视网膜显示屏上提供 2x 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9948493/

相关文章:

iphone - 报亭应用程序需要推送通知吗?

iphone - 我如何分配应用程序来打开邮件附件

ios - 核心数据与 NSFetchedResultsController : Updating Data After Changes

iphone - UIView 以编程方式分配,未在我给出的 x 和 y 处分配。超越这一点。

iOS自适应 View ;自适应约束?

iphone - 为什么我的网络事件图标没有出现在我的 iPhone 应用程序中

iphone - 使用 NSTimer 使按钮在一段时间后可见

iOS:容器 View - 更改 subview Controller 时动画推送过渡

javascript - 如何在 uiwebview 中捕获 javascript 按钮操作

iphone - CGPoint 等价于整数?