0

2 つの UIImageViews があり、画像を一方から他方にコピーして色を調整したいのですが、奇妙なストレッチが発生しています。

コピーを作成するために使用するコードは次のとおりです。

_selectedIcon.image = groupView.icon;
[ViewUtils colorImageView:_selectedIcon withRed:0.843 Green:0.3176 andBlue:0.066667];

最初の行のみを使用すると、画像が正常にコピーされ、次のようになります。

正しい

強調表示するためのものなので、少し大きくなっています。ただし、コピーして正しくスケーリングします。次に、色を付けようとすると、次のようになります。

違う

画像に色を付けるために使用するコードは次のとおりです。

+(void)colorImageView:(UIImageView*)source withRed:(CGFloat)red Green:(float)green andBlue:(float)blue
{
    // Create a context containing the image.
    UIGraphicsBeginImageContext(source.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // The mask needs to be upside down for some reason
    CGContextTranslateCTM(context, 0, source.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextClipToMask(context, source.bounds, [source.image CGImage]);
    CGContextFillRect(context, source.bounds);

    [[UIColor colorWithRed:red green:green blue:blue alpha:1.0] set];
    UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:source.bounds];
    [imagePath fill];
    // Retrieve the new image.
    source.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

着色コードで画像をスケーリングするさまざまな方法を試しましたが、何も機能しないようです。誰か提案がありますか?

4

1 に答える 1

0

私はそれを考え出した。画像は UIImageView 全体に合わせて伸縮していたので、正しくスケーリングされるように境界を調整する必要がありました。使用したコードは次のとおりです。

float ratioX = source.bounds.size.width / source.image.size.width;
float ratioY = source.bounds.size.height / source.image.size.height;
float ratio = MIN(ratioX, ratioY);
CGRect bounds = CGRectMake(0, 0, source.image.size.width * ratio, source.image.size.height * ratio);
于 2013-03-06T18:03:22.220 に答える