Majster
Majster

Reputation: 3701

iOS programatically taking a screen shot messes up the image

I want to take a screen shot every time user holds the screen for one second and then open up an email window to let the user send the image. But I have a strange problem; the image gets messed up if a gradient image is present (UIImage which has a PNG gradient loaded in it - will explain).

So I created a UILongPressGestureRecgnizer, set it's minimumPressDuration to 1.0f and added it to the main view as the gesture recognizer: [self.view addGestureRecognizer:myRecognizer]. The recognizer calls a method, lets say shareClicked. In which I want to capture the current screen and pop up the email composer with that image in it. Here is how I do it:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(self.view.bounds.size);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"Share feature"];
    [mailComposer setMessageBody:[NSString stringWithFormat:@"Im sharing this with you cause its cool."] isHTML:NO];
    [mailComposer addAttachmentData:UIImagePNGRepresentation(image) mimeType:@"image/png" fileName:@"image"];

    [self presentViewController:mailComposer animated:YES completion:NULL];

BUT, there is a problem. There is a table view present on the main view and in the third cell there is a gradient I mentioned earlier. Im not sure how to explain it but its something like this:

enter image description here

As visible on the image there is a gradient on the top and there is another one a bit lower. The top one gets rendered normally while the second one causes a strange problem. The gradient is not actually blue but its white and goes to transparent. Here is an image of the gradient on black surface: enter image description here

Here is a screen shot from the simulator how it should look:

enter image description here

And finally here is how it gets rendered and displayed in the mail composer:

enter image description here

What am I doing wrong? What is the problem? Its not simulator's fault cause it's the same on mobile device as well. Its iOS7 if that makes any difference.

Just to be clear the top gradient is the same but radial and its rendered perfectly. Both gradients are subviews to tableview's cell. How to fix it? Hopefully we find a solution as I did spend some time to "craft" this question :)

Upvotes: 1

Views: 428

Answers (1)

Hooda
Hooda

Reputation: 1187

Have a look to the recomended solution https://developer.apple.com/library/ios/qa/qa1703/_index.html#//apple_ref/doc/uid/DTS40010193 as UIGetScreenImage is no more allowed

Upvotes: 1

Related Questions