AVMakeRectWithAspectRatioInsideRect 返回正确的图像但大小错误

AVMakeRectWithAspectRatioInsideRect returning correct image but wrong size

我是 iOS Swift 开发的新手,但对处理图像更陌生。我在 UIScrollView 中有一个 UIImageView。我希望用户 zoom/pan 显示他们想要捕获的确切图像。我有这个工作在一定程度上。当用户单击一个按钮时,它会正确捕获图像以及 UIScrollView 框中的确切内容,但是,图像的大小不正确。任何帮助将不胜感激!!

@IBAction func tapSendtoBand(sender: UIButton) {


    var frameInfo = frameForImage(photoImageView.image!, imageView: photoImageView)
    var imageWidth = photoImageView.image!.size.height
    var scale:CGFloat = CGFloat(1.0) / (photoImageView.frame.size.height / imageWidth)


    var visibleRect = CGRect()
    var pWidth  = CGFloat(CGImageGetWidth(photoImageView.image?.CGImage))
    var pHeight = CGFloat(CGImageGetHeight(photoImageView.image?.CGImage))


    visibleRect.origin = scrollView.contentOffset
    visibleRect.size = scrollView.bounds.size
    visibleRect.origin.x *= scale
    visibleRect.origin.y *= scale
    visibleRect.size.width *= scale
    visibleRect.size.height *= scale

    var pSize:CGSize = CGSize(width: visibleRect.size.width,height: visibleRect.size.height)


    let bRect = AVMakeRectWithAspectRatioInsideRect(pSize, visibleRect)

    UIImageWriteToSavedPhotosAlbum(cropImage(photoImageView.image!, rect: bRect), nil, nil, nil)

    photoTest.image = cropImage(photoImageView.image!, rect: bRect)

}

这是我用来确定尺寸的框架函数

func frameForImage (image:UIImage, imageView:UIImageView) -> CGRect{

        var imageRatio = image.size.width / image.size.height;
        var viewRatio = imageView.frame.size.width / imageView.frame.size.height;

        if(imageRatio < viewRatio){

            var scale = imageView.frame.size.height / image.size.height;

            var width = scale * image.size.width;

            var topLeftX = (imageView.frame.size.width - width) * 0.5;
            return CGRectMake(topLeftX, 0, width, imageView.frame.size.height)
        }
        else{
            var scale = imageView.frame.size.width / image.size.width;
            var height = scale * image.size.height;
            var topLeftY = (imageView.frame.size.height - height) * 0.5;

            return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
        }
    }

这是我实际用来裁剪图像的函数

func cropImage(srcImage:UIImage,rect:CGRect) -> UIImage
{
    var cgImageConv = srcImage.CGImage
    var cgSizeConv:CGSize = CGSize(width: 310,height: 102)
    var cr:CGImageRef = CGImageCreateWithImageInRect(cgImageConv, rect)
    var cropped:UIImage = UIImage(CGImage: cr)!

    return cropped
}

下面的代码将图像视图的模式设置为宽高比并填充屏幕区域。

//Makes your image view fill the real estate you have available on your phones screen
yourUIImageView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth

\

//Will make your image view Aspect ratio
yourUIImageView.contentMode = UIViewContentMode.ScaleAspectFit

其实我也想通了。这是我的方法。将我的代码用于我想要捕获的可见区域,我能够使用以下代码以正确的比例获得我想要的确切尺寸。

var cr = CGImageCreateWithImageInRect(cgImageConv, visibleRect)
        var cfUIImage = UIImage(CGImage: cr)

        UIGraphicsBeginImageContext(CGSize(width: 620, height: 204))
            var context = UIGraphicsGetCurrentContext();
            CGContextTranslateCTM(context, 0.0, 102);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextDrawImage(context, CGRectMake(CGFloat(0.0), CGFloat(0.0), 310, 102), cr);
            var scaledImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();


        photoTest.image = scaledImage
        UIImageWriteToSavedPhotosAlbum(scaledImage, nil, nil, nil)

Swift 3 关于之前post的解决办法在这里:

func frameForImageInImageViewAspectFit(imageView : UIImageView) -> CGRect {
    let img = imageView.image
    let frame = imageView.frame

    let imageRatio = (img?.size.width)! / (img?.size.height)!;
    let viewRatio = frame.size.width / frame.size.height;

    if (imageRatio < viewRatio) {
        let scale = frame.size.height / (img?.size.height)!;

        let width = scale * (img?.size.width)!;

        let topLeftX = (frame.size.width - width) * 0.5;

        return CGRect(x: topLeftX, y: 0, width: width, height: frame.size.height)
    }
    else {
        let scale = frame.size.width / (img?.size.width)!;

        let height = scale * (img?.size.height)!;

        let topLeftY = (frame.size.height - height) * 0.5;

        return CGRect(x: 0, y: topLeftY, width: frame.size.width, height: height)
    }
}

但更好的解决方案是 (Swift 3):

self.imageView.frame = AVMakeRect(aspectRatio: (self.imageView.image?.size)!, insideRect: self.imageView.frame)