Question

I have a modal view where I want to present the results of a calculation. In order to make it all iOS7 look, I want a tinted background with a popup view with blured background.

I managed to got that thing to work using the files "UIImage+ImageEffects .h/m" from apple.

This is the viewDidLoad of the modal view:

- (void)viewDidLoad {

    [super viewDidLoad];

    /* How it works

     self.image is a screenshot of the VC presenting the modal view

     1- we set the background as the screenshot of the previous viewControler
     2- we hide the popup view
     3- we set the background of the popup as a blured snapshot of its container view    (self.view)
     4- we unhide the popup view and set its radius
     5- we create a tinted version of the background image (what we got from the previous view controller
     6- we set the background of self.view to the tinted version


     */

    // ***  1  ***

    self.view.backgroundColor=[UIColor colorWithPatternImage:self.image];


     // ***  2  ***

    self.containerView.hidden=YES;


    // ***  3  ***

    UIImage *pepe=[self bluredImageFromView:self.containerView withBackground:self.view withBackgroundTintColor:[UIColor colorWithWhite:1 alpha:0.75]];
    self.containerView.backgroundColor=[UIColor colorWithPatternImage:pepe];

    // ***  4  ***

    self.containerView.hidden=NO;
    self.containerView.layer.cornerRadius=4.5f;

    // ***  5  ***

    UIImage *tintedBackground =[self.image applyBlurWithRadius:0
                                                      tintColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:0.35]
                                          saturationDeltaFactor:1.8
                                                      maskImage:nil];
    // ***  6  ***

    self.view.backgroundColor=[UIColor colorWithPatternImage:tintedBackground];


}

it works very well on the simulators but when I tried onmy iPhone (4S), when the modal view is presented, I've got a black screen. No errors, no complaining from the console, no nothing.

Any thoughts?

EDIT

I add the code:

if (!self.image) NSLog (@"self.image is empty");

at the very beginning of viewDidLoad

It seems that on the simulator self.image is not nil whereas on the device it is. I've tried to instantiate self.image from prepareForSegue on the parent VC but doesn't work.

Was it helpful?

Solution

I don't know how your bluredImageFromView is working, but there are security(sandbox) differences between the simulator and an actual iOS device (particularly with resizableSnapshotViewFromRect: and drawViewHierarchyInRect:) which might explain why it works (but then doesn't on a device.)
Check out this thread at raywenderlich.com where someone got advice from Apple Developer tech support. This example code is apparently from Apple.

- (IBAction)doBlurAndCrop:(id)sender {

UIImage *snapshotImage;

    /* draw the image of all the views below our view */
UIGraphicsBeginImageContextWithOptions(self.sourceImageView.bounds.size, NO, 0);
BOOL successfulDrawHierarchy = [self.sourceImageView drawViewHierarchyInRect:self.sourceImageView.bounds afterScreenUpdates:YES];
if ( successfulDrawHierarchy ) {
    snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
} else {
    NSLog(@"drawViewHierarchyInRect:afterScreenUpdates: failed - there's nothing to draw...");
}
UIGraphicsEndImageContext();

if ( successfulDrawHierarchy ) {

        /* calculate the coordinates of the rectangle we're interested in within the returned image */
    CGRect cropRect = CGRectOffset(self.targetImageView.frame, - self.sourceImageView.frame.origin.x, - self.sourceImageView.frame.origin.y);

        /* draw the cropped section with a clipping region */
    UIGraphicsBeginImageContextWithOptions(cropRect.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClipToRect(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height));
    CGRect targetRectangeForCrop = CGRectMake(-cropRect.origin.x, -cropRect.origin.y, snapshotImage.size.width, snapshotImage.size.height);
    [snapshotImage drawInRect:targetRectangeForCrop];
    UIImage *croppedSnapshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

        /* apply a special effect to the resulting image and copy it into place on screen */
    UIColor *tintColor = [UIColor colorWithWhite:1.0 alpha:0.3];
    self.targetImageView.image = [croppedSnapshotImage applyBlurWithRadius:5 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top