3

In my IOS App I am taking screenshot of UIImageView. It's very perfect as shown in attachment Photo. But here, I have taken UIImageView content mode aspect fit . I've used this code.

func captureView() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false,UIScreen.mainScreen().scale)//add this line

        let context: CGContextRef = UIGraphicsGetCurrentContext()!
        self.backView.layer.renderInContext(context)
        let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }

Simply I need the snapshot of Image, Not the ImageView. Help me into this.enter image description here

JAck
  • 854
  • 9
  • 18
  • Try [this](http://stackoverflow.com/a/6284498/790842) to get the exact size of the image, and supply that size to the code when you are making screenshot, though you will need to adjust the `frame.x` too. – iphonic Aug 02 '16 at 09:19
  • Get the image of an image view, `imageView.image` – zylenv Aug 02 '16 at 09:42
  • @zylenv not trying to make that kind of easy thing – JAck Aug 02 '16 at 09:46
  • i didn't get whats your problem ? – srinivas n Aug 02 '16 at 10:20
  • It's screenshot of whole imageView I need only Image as Screenshot – JAck Aug 02 '16 at 10:42
  • @JAck try the solution what I suggested it just you need to get the frame of shown image and supply the frame to take screenshot, it should work. – iphonic Aug 02 '16 at 11:13
  • i've tried but the problem in getting x & y. I got the size of that that is nice algorithm – JAck Aug 02 '16 at 11:30
  • `x = imageView.frame.size.width - calculatedSize.width;` `y=0;` this will give close calculation, try this. – iphonic Aug 02 '16 at 11:39
  • hey .. This algo is not giving me perfect value in same example my imageview frame have 320 , 402 size and image (by this func) returns 302,576 size – JAck Aug 02 '16 at 12:22
  • @JAck have you solved your issue ? i am facing same issue can you please help me? – Khushbu Desai Nov 06 '17 at 10:12

1 Answers1

0

Try this code:

func captureView() -> UIImage() {

    UIGraphicsBeginImageContext(YourimageView.frame.size)
    YourimageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let img = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil)
    return img
}
Pranav
  • 701
  • 4
  • 18