1

画像をトリミングするアプリを開発しています。次のコードを使用して、トリミングのメソッドを呼び出しています。

UIImage* croppedImage = [self imageCrop:imageView toRect:CGRectMake(10.0, 50.0, 320, 100)];

メソッドのコードは次のとおりです。

{
    //create a context to do our clipping in
    CGRect newRect = CGRectApplyAffineTransform(rect, imageViewToCrop.transform);
    UIImage *imageToCrop = imageViewToCrop.image;
    UIGraphicsBeginImageContext(newRect.size);
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    //create a rect with the size we want to crop the image to
    //the X and Y here are zero so we start at the beginning of our
    //newly created context
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
    CGContextClipToRect( currentContext, clippedRect);

    //draw the image to our clipped context using our offset rect
    CGContextDrawImage(currentContext, newRect, imageToCrop.CGImage);

    //pull the image from our cropped context
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

    //pop the context to get back to the default
    UIGraphicsEndImageContext();

    return cropped;
}

問題は、この画像を UIImageView に設定すると、返された画像が左に回転していることがわかりました。そして、私はそれを回転させることができません。誰でも問題を知ることができますか?

4

2 に答える 2

2

Uiimage にはimageOrientationプロパティがあり、システムがイメージのビットを表示する方法を示す場合があります。その値をログに記録して確認してください。私が思うに、切り抜きアルゴリズムを調整する必要があるかもしれません。最初に CGImage を作成してから、UIImage メソッドを使用して、向きパラメータを持つ UIImage を作成します。

于 2013-04-21T12:30:36.837 に答える
1

David が書いたように、問題はimageOrientationプロパティを保持していない可能性があります。また、コードの問題は、スケールプロパティを保持しないことです。次のようにトリミングした画像を作成してみてください。

CGImageRef croppedRef = CGBitmapContextCreateImage(currentContext);
UIImage* cropped = [UIImage imageWithCGImage:croppedRef scale:imageToCrop.scale orientation:imageToCrop.imageOrientation];
CGImageRelease(croppedRef);
于 2013-04-21T12:42:16.490 に答える