2012-03-30 4 views
2

私は画像を作成し、それにいくつかの効果を加えてそれをサイズダウンしてlargeThumbnailにするこのコードを持っています。後でUIImageJPEG網膜上に2倍の画像を表示する様子

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

なぜ起こっているのでしょうか?あなた自身でこれをデバッグしようとしましたか? –

+0

私はNSLogを追加しました。それは、NSDataからイメージが元に戻ったところで起こることを明らかにしました。 – Andrew

答えて

6

あなたは第二の画像読み込みの部分交換してみてください:この1と

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

+0

"縮尺が設定されていないので、二重画像の寸法を取得します。" ..この行は私に役立ちます...ありがとうございます。 – IKKA

+0

縮尺として "0"を渡して、縮尺変更画像を作成していました。合格するとoriginalImage.scaleはそれを修正しました。ありがとう!! – PaulG

関連する問題