0

I have an UIImageView that fits the size of the screen:

imageView.contentMode = .scaleAspectFill
self.view.addSubview(imageView)
imageView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
imageView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

And inside that imageView I have an image:

imageView.image = UIImage(named: "pizza")

At a certain position within the imageView's bounds I want to extract what's inside a part of the pizza and send that extracted part to another UIView.

For example:

let extractionPointX = 0 //imageView.bounds.orgin.x == 0
let extractionPointY = 50 //imageView.bounds.orgin.y == 50
let extractionHeight = 100 //area's height is == 100
let extractionWidth = imageView.frame.size.width

let myExtractedImage = //the corrdinates from above

So now that I have that x, y, height, and width of whatever that area is on the picture of the pizza, I can now take that and add it to another UIView.

anotherView.addSubview(myExtractedImage)

or

anotherImageView.image = UIImage(named: "myExtractedImage")

I don't care about what happens to the image of the pizza afterwards because I'm going to set the imageView's image to nil once I get the area I need extracted from it. It can be the actual area of the pizza cut out as a UIView or or a separate image of that area (like a snapshot) of it.

How would I do this?

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • "At a certain position within the imageView's bounds I want to extract what's inside a part of the pizza and send that extracted part to another UIView." Sounds like you are describing cropping the image. Did you search on "crop"? It's been explained many times here. – matt Mar 18 '18 at 20:50
  • @matt thank you sir. I will look it up. I was googling everything except crop. Thanks – Lance Samaria Mar 18 '18 at 20:52
  • Please see the linked question. I'm pretty sure my answer there is exactly what's you're asking for. – matt Mar 18 '18 at 20:58
  • I’m looking at it now, thanks – Lance Samaria Mar 18 '18 at 20:58
  • Cropping or are you looking for the actual rect of the image that is being occupied in the UIImageView? – agibson007 Mar 19 '18 at 00:36
  • @agibson007 thanks. I am looking for the rect of the image that I'm trying to crop. – Lance Samaria Mar 19 '18 at 00:46
  • I cannot add the answer because it has been marked as a duplicate but Matts answer that he was so quickly to link is not your answer – agibson007 Mar 19 '18 at 00:59
  • @agibson007 can you add to a similar answer? I was just looking at it now. https://stackoverflow.com/questions/10859717/how-to-crop-a-uiimageview-to-a-new-uiimage-in-aspect-fit-mode?rq=1 – Lance Samaria Mar 19 '18 at 01:00
  • @agibson007 those answers are all in ObjC so a Swift answer would actually be very helpful over there – Lance Samaria Mar 19 '18 at 01:01
  • 1
    So if its the crop there are swift methods. if it is just the rect the image occupies in an aspect fit UIImageView see this. https://stackoverflow.com/a/49354467/3641744 – agibson007 Mar 19 '18 at 01:12
  • @agibson007 thank you very much for the help. I'm looking now :) – Lance Samaria Mar 19 '18 at 01:12
  • Did that help you or was that not what you are after? – agibson007 Mar 19 '18 at 02:04
  • @agibson007 between that and matt's answer and looking at a bunch of other answer's I finally figured it out. It 8-10 to figure out. Thank you very much for the link, it helped :) – Lance Samaria Mar 19 '18 at 02:15

0 Answers0