2012-03-30 75 views
2

我有这段代码,它会创建一个图像,然后向它添加一些效果并将其缩小到largeThumbnailUIImageJPEGRepresentation在视网膜显示屏上提供2 x图像

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

请问_you_有任何想法,为什么它可能会发生?你有什么试图自己调试呢? – 2012-03-30 19:15:46

+0

我已经把额外的NSLogs,它显示它发生在图像从NSData转换回来的点。 – Andrew 2012-03-30 19:20:17

回答

6

尝试更换在这里装载第二图像的部分:

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

这一个:

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

这里会发生什么事是,图像比例没有设置,所以你会得到双重图像尺寸。

+0

我有同样的问题(从NSData *转换回来时获得2倍图像)。你能详细说明吗?对我而言,'originalImage.scale'返回1,所以这个解决方案对我来说不起作用。 “ – mostruash 2013-06-08 17:00:23

+0

”没有设置比例尺,所以你会得到双倍的图像尺寸。“..这条线帮助我...谢谢.. – IKKA 2014-11-07 06:42:50

+0

我传递”0“作为缩放到UIGraphicsBeginImageContextWithOptions来创建缩放图像。通过originalImage.scale修复它。谢谢!! – PaulG 2015-12-15 15:25:31