如何从旋转的UIImageView获取UIImage的精确边界框

时间:2013-12-12 17:43:37

标签: ios iphone objective-c uiimageview uiimage

我将转换应用于UIImageView(旋转,缩放和反转),我想从旋转的UIImageView中保存UIImage。那么如何才能获得UIImage的精确边界框来绘制UIImage视图。

    CGAffineTransform transform = selImageView.transform;
    CGPathRef rotatedImageRectPath = CGPathCreateWithRect(selImageView.frame, &transform);
    CGRect boundingBox = CGPathGetBoundingBox(rotatedImageRectPath);

    CGSize rotatedSize = boundingBox.size;
    UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 0.0f);

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), rotatedSize.width/2, rotatedSize.height/2);
//    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContex(),[UIColor clearColor].CGColor);
    //Rotate the image context using tranform
    CGContextConcatCTM(UIGraphicsGetCurrentContext(), selImageView.transform);
    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(-aTransImage.size.width / 2, -aTransImage.size.height / 2, aTransImage.size.width, aTransImage.size.height), [aTransImage CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

1 个答案:

答案 0 :(得分:0)

查看此应用。它完成了您尝试执行的大部分任务。你可以分析工作代码。

https://github.com/heitorfr/ios-image-editor