iOS-NBUImagePicker icon indicating copy to clipboard operation
iOS-NBUImagePicker copied to clipboard

Application freezes after taking many pictures

Open aseemss opened this issue 10 years ago • 3 comments

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

aseemss avatar Jul 19 '15 05:07 aseemss

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.

rivera-ernesto avatar Jul 20 '15 13:07 rivera-ernesto

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?

aseemss avatar Jul 21 '15 05:07 aseemss

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.

rivera-ernesto avatar Jul 21 '15 13:07 rivera-ernesto