3

だから私はUIImageを作成するためのこのコードを持っています:

UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0);
[border.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

この時点で、画像のサイズは正しい 80x100 です。

次に、次のコードを実行します。

NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f);

そして、画像の NSData は 160x200 のサイズの画像を返します。

この理由が次の行であることが明らかになりました。

UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0);

末尾の 0 はスケールです。これは 0 であるため、デバイスのスケール ファクターに従います。鮮明なイメージを維持するために、このようにしています。ただし、画像を1に設定すると、画像は本来のサイズのままですが、網膜の品質にはなりません。私がやりたいのは、網膜の品質を維持するだけでなく、適切なサイズに維持することです。これを行う方法はありますか?

4

2 に答える 2

2

UIImageJPEGRepresentation を呼び出す前に、UIImage のサイズを変更してみてください

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);  
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();    

    return newImage;
}

if([UIScreen mainScreen].scale > 1)
    {
        thumbnailImage = [self thumbnailImage newSize:CGSizeMake(thumbnailImage.size.width/[UIScreen       mainScreen].scale, thumbnailImage.size.height/[UIScreen mainScreen].scale)];
    }
于 2012-06-11T13:02:01.217 に答える