Search code examples
iosobjective-ciphoneuiviewcontrollerphoto-gallery

Loss of quality passing images from viewcontroller iOS


i'm developing an application that allows users to add masks at their photos, the app works well but i noticed a problem, i don't know why but the image loses quality. i'm passing images to view controllers by using this method:

   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//SETTO STICKERS
UIGraphicsBeginImageContext(CGSizeMake(_Image.frame.size.width, _Image.frame.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
[_Image.layer renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

appDelegate.ImageEnded = screenShot;
//Instanzio il viewController della main
LastViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"LastVC"];
//Vado alla main
[self presentModalViewController:VC animated:YES];

and by take the image from the other ViewController using the appdelegate, here is my full app-process:

FirstScreen(ViewController) -> TakePhoto or Select from gallery ->
AddMask(ViewController) -> AddStickers(ViewController) -> LastViewcontroller(ViewController) .


Solution

  • By using UIGraphicsBeginImageContext , the scale factor parameter defaults to 1.0.

    As per the docs (emphasis mine):

    Discussion This function is equivalent to calling the UIGraphicsBeginImageContextWithOptions function with the opaque parameter set to NO and a scale factor of 1.0.

    If you use it on a retina device, you'll see some quality loss.

    You should use UIGraphicsBeginImageContextWithOptions, and pass in 0.0 for the scale factor, which will default to the current main screen's scale factor. (1 for nonretina, 2 for retina devices).