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

Application freezes after taking many pictures #14

Open
aseemsandhu opened this issue Jul 19, 2015 · 3 comments
Open

Application freezes after taking many pictures #14

aseemsandhu opened this issue Jul 19, 2015 · 3 comments
Labels

Comments

@aseemsandhu
Copy link

The application freezes after taking/posting many pictures. UI locks up, basically. No exception, though. Any suggestions on fix?

@rivera-ernesto
Copy link
Contributor

Does it happen with the demo app?

More generally, you may be doing some picture processing in the main thread, try using Grand Dispatch Central to do it in other threads with lower priority.

@aseemsandhu
Copy link
Author

I was not able to get down to the bottom of what seems like a deadlock. I am doing some image processing on the main thread. Here is my code:

[NBUImagePickerController startPickerWithTarget:self options:options customStoryboard:nil resultBlock:^(NSArray *imagesOrMediaInfos) {

    id mediaObject = [imagesOrMediaInfos firstObject];
    if ([mediaObject isKindOfClass:[UIImage class]]) {

        UIImage *image = (UIImage *)mediaObject;
        UIImage *transformedImage = [image transformToSquareImage];

        [self.btnSelectPhoto setImage:nil forState:UIControlStateNormal];

        self.imageView.image = transformedImage;

        self.pngData = UIImageJPEGRepresentation(transformedImage, 0.9);
    }
}];

And here is the method in my UIImage category:

- (UIImage *)transformToSquareImage {

     float currentImageWidth = self.size.width;
     float currentImageHeight = self.size.height;

     float diff = currentImageWidth - currentImageHeight;

     if (diff == 0) {
         return self;
     }

     CGSize desiredSize = CGSizeMake(MAX(currentImageWidth, currentImageHeight), MAX(currentImageWidth, currentImageHeight));

     UIGraphicsBeginImageContext(desiredSize);

     UIImage *image1;
     UIImage *image2;

     if (diff > 0) {

         // Width is greater than the height
         image1 = [OCCommon createImageWithColor:[UIColor blackColor] size:CGSizeMake(currentImageWidth, diff / 2)];
         image2 = [OCCommon createImageWithColor:[UIColor blackColor] size:CGSizeMake(currentImageWidth, diff / 2)];
         [image1 drawInRect:CGRectMake(0 ,0, image1.size.width, image1.size.height)];
         [self drawInRect:CGRectMake(0, image1.size.height, currentImageWidth, currentImageHeight)];
         [image2 drawInRect:CGRectMake(0, image1.size.height + currentImageHeight, image2.size.width, image2.size.height)];

     } else if (diff < 0) {

         // Height is greater than the width
         image1 = [OCCommon createImageWithColor:[UIColor blackColor] size:CGSizeMake(-diff / 2, currentImageHeight)];
         image2 = [OCCommon createImageWithColor:[UIColor blackColor] size:CGSizeMake(-diff / 2, currentImageHeight)];
         [image1 drawInRect:CGRectMake(0 ,0, image1.size.width, image1.size.height)];
         [self drawInRect:CGRectMake(image1.size.width, 0, currentImageWidth, currentImageHeight)];
         [image2 drawInRect:CGRectMake(image1.size.width + currentImageWidth, 0, image2.size.width, image2.size.height)];

     }

     UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();

     return finalImage;
}

With this code, I would assume that the main thread would only temporarily be blocked until the processing is finished. Am I missing something?

@rivera-ernesto
Copy link
Contributor

Yes, it should "just" temporarily freeze your app, which isn't good, but shouldn't stop the app altogether.

Try debugging in Xcode by hitting pause when your app gets frozen. Then check the stack trace to see what it was doing.

2015-07-21 9 17 35

As for the picker, once it calls your resultBlock it is dismissed and shouldn't be doing anything else. In other words, I'm pretty sure it's on your side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants