Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in background thread #76

Closed
mbcoder17 opened this issue Mar 9, 2015 · 2 comments
Closed

Crash in background thread #76

mbcoder17 opened this issue Mar 9, 2015 · 2 comments

Comments

@mbcoder17
Copy link

I am trying to display an SCLAlertView in my app, but I am getting this error:

Thread 1: EXC_BAD_ACCESS (code=EXC_1386_GPFLT)

on this line [keyWindow.layer renderInContext:context];

I am creating the alert after I call one of my parse cloud code functions. I have read that renderInContext should not be called in a background thread, but I have this exact same code in a different view controller (also after I call a cloud code function), and the alert displays perfectly.

Also, if I display and hide a regular UIAlertView immediately before I show this one, the SCLAlertView shows without a problem, but the view behind it is a solid white. Why is this happening? Is UIAlertView somehow breaking out of the background thread?

I have tried displaying the SCLAlertView in a background thread as suggested here: http://stackoverflow.com/questions/15135208/objective-c-renderincontext-crash-on-background-thread but the app still crashed.

Here is my code to display the alert:

[PFCloud callFunctionInBackground:@"canWeStart"
                           withParameters:@{@"inviteId": self.selectedObject.objectId,
                                            @"gameType": [[self.selectedObject objectForKey:@"GameType"] objectForKey:@"type"]}
                                    block:^(NSArray *startStuff, NSError *error) {
                                        if (!error) {

                                            NSNumber *canStart = startStuff[0];

                                            BOOL startBool = [canStart boolValue];

                                            if(startBool)
                                            {
                                               //Stuff

                                            }
                                            else
                                            {

                                                //If I show this first, my alert shows perfectly.
                                                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Others" message:@"Need to accept" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                                                [alert show];

                                                [alert dismissWithClickedButtonIndex:0 animated:NO];

                                                SCLAlertView *alert = 

[[SCLAlertView alloc] init];

        alert.backgroundType = Blur;

        //button.layer.borderWidth = 2.0f;

        [alert setTitleFontFamily:@"Alba" withSize:18.0f];

        [alert setBodyTextFontFamily:@"Arista 2.0" withSize:14.0f];

        [alert setButtonsTextFontFamily:@"Arista 2.0 Light" withSize:14.0f];

        alert.customViewColor = [UIColor pxColorWithHexValue:@"2b3355"];

        alert.backgroundViewColor = [UIColor whiteColor];

        [alert showNotice:self.navigationController title:@"Invite Accepted" subTitle:@"Others still need to accept the invite, then the game will begin!" closeButtonTitle:@"Ok" duration:0.0f];

                                                }

                                            }

                                        }];
@dogo
Copy link
Owner

dogo commented Mar 9, 2015

Please test the following code:

Replace makeBlurBackground by :

- (void)makeBlurBackground
{
    UIImage *image = [UIImage convertViewToImage:_rootViewController.view];
    UIImage *blurSnapshotImage = [image applyBlurWithRadius:5.0f
                                         tintColor:[UIColor colorWithWhite:0.2f
                                                                     alpha:0.7f]
                             saturationDeltaFactor:1.8f
                                         maskImage:nil];

    _backgroundView.image = blurSnapshotImage;
    _backgroundView.alpha = 0.0f;
    _backgroundOpacity = 1.0f;
}

Replace convertViewToImage by :

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
+ (UIImage*)convertViewToImage:(UIView *)view
{
    CGFloat scale = [UIScreen mainScreen].scale;
    UIImage *capturedScreen;

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        //Optimized/fast method for rendering a UIView as image on iOS 7 and later versions.
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, scale);
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
        capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    else
    {
        //For devices running on earlier iOS versions.
        UIGraphicsBeginImageContextWithOptions(view.bounds.size,YES, scale);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
        capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    return capturedScreen;
}

@mbcoder17
Copy link
Author

Hi, thank you for the very quick response! I replaced both of those methods and the alert now displays. Thank you very much! :)

dogo added a commit that referenced this issue Mar 10, 2015
@dogo dogo closed this as completed Mar 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants