30

次のコードを使用して、読み込んだ画像に対していくつかの操作を実行していますが、Retina ディスプレイ上にあると表示がぼやけることがわかりました

- (UIImage*)createImageSection:(UIImage*)image section:(CGRect)section

{
float originalWidth = image.size.width ;
float originalHeight = image.size.height ;
int w = originalWidth * section.size.width;
int h = originalHeight * section.size.height;

CGContextRef ctx = CGBitmapContextCreate(nil, w, h, 8, w * 8,CGImageGetColorSpace([image CGImage]), kCGImageAlphaPremultipliedLast);
CGContextClearRect(ctx, CGRectMake(0, 0, originalWidth * section.size.width, originalHeight * section.size.height)); // w + h before
CGContextTranslateCTM(ctx, (float)-originalWidth * section.origin.x, (float)-originalHeight * section.origin.y);
CGContextDrawImage(ctx, CGRectMake(0, 0, originalWidth, originalHeight), [image CGImage]);
CGImageRef cgImage = CGBitmapContextCreateImage(ctx);
UIImage* resultImage = [[UIImage alloc] initWithCGImage:cgImage];

CGContextRelease(ctx);
CGImageRelease(cgImage);

return resultImage;
}

これを変更して Retina 対応にする方法を教えてください。

よろしくお願いいたします。

最高、DV

4

3 に答える 3

48

CGImages はデバイスの Retina 性を考慮していないため、自分で行う必要があります。

これを行うには、CoreGraphics ルーチンで使用するすべての座標とサイズを入力画像のscaleプロパティ (Retina デバイスでは 2.0 になります) で乗算して、すべての操作を 2 倍の解像度で確実に行う必要があります。

次に、同じ倍率resultImageを使用して入力するように初期化を変更する必要があります。initWithCGImage:scale:orientation:これが、Retina デバイスがピクセル倍の解像度ではなくネイティブ解像度で出力をレンダリングする理由です。

于 2011-01-16T19:43:27.047 に答える
2

迅速なバージョン:

    let scale = UIScreen.mainScreen().scale

    CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale)
于 2016-01-06T09:26:08.700 に答える