我有这段代码,它创建一个图像,然后向它添加一些效果并将其缩小以制作 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/