2012-03-30 77 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