0

CGAffineTransformを使用せずにコンテキストのサイズよりも小さい画像を描画する方法は?

UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// center image
CGContextTranslateCTM(context, placement.origin.x, placement.origin.y);
[imageView.layer renderInContext:context];

問題は、それimageView.image.sizeよりも大きい場合はsize中央に描画されますが、境界線でカットされることです (描画領域よりも大きな画像)。画像を合わせたいので、 のサイズを小さくしたいimageView.image.size。を使用してこれを達成する方法はありますrenderInContextか?

4

3 に答える 3

0

サイズ[imageView.layer renderInContext:context];を変更することはできません(または方法が見つかりませんでした)。これを行う唯一のオプションは、(要求されたサイズで) drawImage を使用して画像を作成し、新しい UIImageView を割り当て、そのレイヤーを描画することです。

UIImage *resizedImage = [self createNewImageFrom:currentImage withSize:size];
UIImageView *imageView = [[UIImageView alloc] initWithImage:resizedImage];
// ...
[imageView.layer renderInContext:context];


- (UIImage*)createNewImageFrom:(UIImage*)image withSize:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0f, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return outputImage;
}
于 2013-07-31T12:05:44.177 に答える
0

コンテキストを変換せずにこれを実行したいのはなぜですか? 仕事に最適なツールです。

ここでの最も簡単な方法はCGContextScaleCTM、コンテキストに倍率を適用するために呼び出すことです。その倍率は、への呼び出しを含む、コンテキストへのすべての描画に影響します-renderIntoContext:

于 2013-07-31T13:21:04.200 に答える
-1

これはスケーリングされたサイズのバージョンです。スケールを size.width と size.height に置き換えて、任意のターゲット サイズにします。

UIView の場合:

-(UIImage*)convertViewToImage:(UIView*)v withScale:(float)scale{
    //create bitmap context with scaled size
    CGSize s = v.bounds.size;
    s.width *= scale;
    s.height *= scale;
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);

    //scale CTM to render and resotre CTM
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextScaleCTM(context, scale, scale);
    [v.layer renderInContext: context];
    CGContextRestoreGState(context);

    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

UIScrollView用

-(UIImage*)convertScrollContentToImage:(UIScrollView*)sv withScale:(float)scale{
    //create bitmap context with scaled size
    CGSize s = sv.contentSize;
    s.width *= scale;
    s.height *= scale;
    UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);


    {
        //set scroll.frame.size to contentSize, and contentOffset to 0
        CGPoint savedContentOffset = sv.contentOffset;
        CGRect savedFrame = sv.frame;

        sv.contentOffset = CGPointZero;
        sv.frame = CGRectMake(0, 0, sv.contentSize.width, sv.contentSize.height);

        //scale CTM to render and resotre CTM
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        CGContextScaleCTM(context, scale, scale);
        [sv.layer renderInContext: context];
        CGContextRestoreGState(context);

        //set scroll.frame.size and ContentOffset back
        sv.contentOffset = savedContentOffset;
        sv.frame = savedFrame;

    }

    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
于 2014-10-26T15:44:29.683 に答える