15

この質問について申し訳ありませんが、SO で多くのスレッドを検索しましたが、何も見つかりませんでした。

問題は、UIImage の中央の正方形をトリミングする方法です。

以下のコードを試しましたが、成功しませんでした。トリミングは行われますが、左上隅にあります。

-(UIImage*)imageCrop:(UIImage*)original
{
    UIImage *ret = nil;

    // This calculates the crop area.

    float originalWidth  = original.size.width;
    float originalHeight = original.size.height;

    float edge = fminf(originalWidth, originalHeight);

    float posX = (originalWidth   - edge) / 2.0f;
    float posY = (originalHeight  - edge) / 2.0f;


    CGRect cropSquare = CGRectMake(posX, posY,
                                   edge, edge);


    // This performs the image cropping.

    CGImageRef imageRef = CGImageCreateWithImageInRect([original CGImage], cropSquare);

    ret = [UIImage imageWithCGImage:imageRef
                              scale:original.scale
                        orientation:original.imageOrientation];

    CGImageRelease(imageRef);

    return ret;
}
4

1 に答える 1

16

さらに情報を追加すると、写真をキャプチャした後にこの「クロップ」を使用しています。

いくつかのテストの後、私はうまくいくものを見つけました。

// If orientation indicates a change to portrait.
if(original.imageOrientation == UIImageOrientationLeft ||
   original.imageOrientation == UIImageOrientationRight)
{
    cropSquare = CGRectMake(posY, posX,
                            edge, edge);

}
else
{
    cropSquare = CGRectMake(posX, posY,
                            edge, edge);
}
于 2012-06-14T18:59:41.560 に答える