0

I want to get the CGRect value of the image after it is being "aspect fitted" onto the screen. All the solutions I found online only gives the CGSize. However, I want the origin so that I can draw a canvas on top of that image to perform drawing only on top of the image rather than the whole imageView.

Thanks,

Code Different
  • 90,614
  • 16
  • 144
  • 163
user172902
  • 3,541
  • 9
  • 32
  • 75
  • Don't have Xcode with me to test it out, but [`AVMakeRectWithAspectRatioInsideRect`](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVFoundation_Functions/index.html#//apple_ref/c/func/AVMakeRectWithAspectRatioInsideRect) may give you what you want – Code Different Jun 28 '16 at 17:10

2 Answers2

0
let rect = CGRectIntegral(AVMakeRectWithAspectRatioInsideRect(imageView.image!.size, imageView.bounds))
Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
CodeBender
  • 35,668
  • 12
  • 125
  • 132
-1

Try to execute below code

var img=UIImageView(frame: CGRectMake(50, 50, 100, 100))
var imageOrigin=img.frame.origin //This will give you image x and y position
print(img.frame.origin) // this will print (50.0, 50.0)
Amit Jagesha シ
  • 1,092
  • 12
  • 21
  • This will not return what I want because when the image is in aspect fit, the image will shrink inside the image.frame. I am looking for the frame of the image inside it not the imageview that contains it – user172902 Jun 28 '16 at 17:31
  • may be you find answer here http://stackoverflow.com/questions/6278876/how-to-know-the-image-size-after-applying-aspect-fit-for-the-image-in-an-uiimage – Amit Jagesha シ Jun 28 '16 at 17:33