user1191140
user1191140

Reputation: 1579

Image appearing pixelerated

This is what I am trying to do

  1. Create a UIImage View.
  2. Do some drawing on it
  3. Press a share button to share the image with the drawings on it.

My code works perfect on iPad and iPhone. Problem comes with retina display. So my guess is some scale is not handled correctly, but not sure what I am doing wrong. This is my code

// Create the UIImageView named centerCanvas
// Do the drawing
CGPoint origin = centerCanvas.frame.origin;
CGSize  size   = centerCanvas.frame.size;
CGSize screenSize = [self returnScreenSize];
CGRect  rect   = CGRectMake(origin.x, origin.y, size.width, size.height);
UIGraphicsBeginImageContext(screenSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], rect);
[shareView setImage:[UIImage imageWithCGImage:imageRef]]; 


-(CGSize) returnScreenSize
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGFloat screenScale = [[UIScreen mainScreen] scale];
    CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale,screenBounds.size.height * screenScale);
    return screenSize;
}

Upvotes: 0

Views: 63

Answers (1)

sergio
sergio

Reputation: 69027

Try using

    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);

instead of the old UIGraphicsBeginImageContext, which uses a scale factor (third argument above) equal to 1. By giving 0.0 as a scale factor you get a scale factor for your current screen. Have a look at the reference.

Upvotes: 1

Related Questions