I wrote this method to get the image viewing frame after loading the image. So, the requirements for me were the same as in your case:
1) view the image with content mode with matching support 2) get the exact frame of the image (this way you can move the image position)
Hope this helps:
- (CGRect) getFrameOfImage:(AsyncImageView *) imgView { if(!imgView.loaded) return CGRectZero; CGSize imgSize = imgView.image.size; CGSize frameSize = imgView.frame.size; CGRect resultFrame; if(imgSize.width < frameSize.width && imgSize.height < frameSize.height) { resultFrame.size = imgSize; } else { float widthRatio = imgSize.width / frameSize.width; float heightRatio = imgSize.height / frameSize.height; float maxRatio = MAX (widthRatio , heightRatio); NSLog(@"widthRatio = %.2f , heightRatio = %.2f , maxRatio = %.2f" , widthRatio , heightRatio , maxRatio); resultFrame.size = CGSizeMake(imgSize.width / maxRatio, imgSize.height / maxRatio); } resultFrame.origin = CGPointMake(imgView.center.x - resultFrame.size.width/2 , imgView.center.y - resultFrame.size.height/2); return resultFrame; }
I use AsyncImageView here, but it will work just as well with UIImageView . It is important to remember that you need to call this method AFTER the image is loaded.
Hooray!
source share