7

AVMakeRectWithAspectRatioInsideRect returns the correct image but an incorrect s...

 2 years ago
source link: https://www.codesd.com/item/avmakerectwithaspectratioinsiderect-returns-the-correct-image-but-an-incorrect-size.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

AVMakeRectWithAspectRatioInsideRect returns the correct image but an incorrect size

advertisements

I am new to iOS Swift development but even newer to manipulating images. I have a UIImageView inside of a UIScrollView. I want a user to zoom/pan to show the exact image they want to capture. I have this working to an extent. When the user clicks a button, it does properly capture the image and exactly what was in the UIScrollView box, however, the SIZE of the image is not correct. Any help would be appreciated!!

@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)

}

Here is the frame function I am using to try to determine the sizing

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);
        }
    }

Here is the function that I use to actually crop the image

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
}


The code below sets the mode for your image view to aspect ratio and to fill area of screen.

//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




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK