0

I have imageView with Aspect fit mode. I have added the another view on the imageView. I want to take a screenshot of the imageView without compressing (as same resolution as before).

I have tried the following code

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(imgView.image.size.width, imgView.image.size.height), NO, 1.0);
    [imgView drawViewHierarchyInRect:CGRectMake(0, 0, imgView.image.size.width, imgView.image.size.height) afterScreenUpdates:YES];
   UIImage *  Image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

But the end image is streach. What should I do?

I think it includes the transparent portion here. is there any way to not to include in that

Cœur
  • 37,241
  • 25
  • 195
  • 267
Raj Sharma
  • 11
  • 4
  • If you are using the retina display then try to increase the height and width of the cropping section by twice or four times and let me if it works. i.e. (imgView.image.size.width*2,imgView.image.size.height * 2) or (imgView.image.size.width*4,imgView.image.size.height * 4). – improgrammer Dec 16 '14 at 17:35
  • http://stackoverflow.com/questions/8858404/uiimage-aspect-fit-when-using-drawinrect – Vitalii Gozhenko Dec 16 '14 at 18:32
  • how you solved your problem ? – Badal Shah Nov 17 '15 at 04:43

2 Answers2

0

try this:

UIGraphicsBeginImageContextWithOptions(imgView.bounds.size, NO, 0.0);

[imgView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *newImageWithMyImageView = UIGraphicsGetImageFromCurrentImageContext();
Onik IV
  • 5,007
  • 2
  • 18
  • 23
0

I had the same problem because the image's height was a decimal, I casted it to integer value and the image is not stretched any more.

So try this:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(imgView.image.size.width, imgView.image.size.height), NO, 1.0);
[imgView drawViewHierarchyInRect:CGRectMake(0, 0, (int) imgView.image.size.width, (int)imgView.image.size.height) afterScreenUpdates:YES];
UIImage *  Image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
Omaty
  • 425
  • 5
  • 14