Kjuly / ALAssetsLibrary-CustomPhotoAlbum

A nice ALAssetsLibrary category for saving images & videos into custom photo album.
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
MIT License
405 stars 101 forks source link

App Freezing when 100+ photos are send for writting #24

Closed tarzon closed 9 years ago

tarzon commented 9 years ago

The application Freezes when 100+ requests are send for writting. Upon faliour i am iterating the same method for writting. Here the code.

- (void)savingDownloadedImages : (UIImage *)img withDictionary:(NSDictionary*)dic {
    NSString *albumName     = dic[kPhotoAlbumName];

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library saveImage:img toAlbum:albumName completion:^(NSURL *assetUrl, NSError *error) {
        if (error) {
            [self savingDownloadedImages:img withDictionary:dic];
        }
    } failure:^(NSError *error){
        [self savingDownloadedImages:img withDictionary:dic];
    }];
}
Kjuly commented 9 years ago

In ur method, u've allocated the ALAssetsLibrary instance every time the method is invoked. I suggest u to reuse it (property or just iVar). Plz let me know if it still freezes then. :)

tarzon commented 9 years ago

Hi @Kjuly Yep the freeze problem is still there even when ALAssetsLibrary *library allocation is done in the singleton. Also i noticed a issue that sometimes the Custom Albums are not created while the photos are getting copied to the Photo Gallery.

Kjuly commented 9 years ago

Got it, will do debugging when I got time. And of course, I'd glad if u figure out the reason & send a pull request. Thx :)

tarzon commented 9 years ago

@Kjuly Thanks. Will try to figure it out as the problem persist irrespective of the IOS Version.

Kjuly commented 9 years ago

Hi @tarzon , sorry I just got the time to take the testing. Can u try to add an async wrap around ur main code? Like:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  NSString *albumName = dic[kPhotoAlbumName];
  [self.library saveImage:img toAlbum:albumName completion:^(NSURL *assetUrl, NSError *error) {
      if (error) {
        [self savingDownloadedImages:img withDictionary:dic];
      }
    } failure:^(NSError *error) {
      [self savingDownloadedImages:img withDictionary:dic];
    }];
};
Kjuly commented 9 years ago

Hi @tarzon, I'll close this issue first, if u've still got the freezing problem, plz let me know & reopen this issue. Thx :)

tarzon commented 9 years ago

Hi @Kjuly Sorry for the late reply, but the issue still persists. Adding the global queue doesn't make any difference.

Kjuly commented 9 years ago

Hi @tarzon , i've tried to add bunch images at same time without freezing problem, could u show me a sample project that has the problem?

B.t.w., r u sure ur code won't die in an infinite loop for -savingDownloadedImages:withDictionary: when got an error? Try to comment out those two lines in completion & failure blocks when saving failed. Saving failed will also make ur app froze because of the infinite loop.

tarzon commented 9 years ago

Hi @Kjuly What should be the correct approach for saving images when count is 100+ if not iterating. Also how should we tackle the failour in order to save each and every image. Most common error that i got is Writer busy.

renjithn commented 9 years ago

In my application I use an NSOperationQueue with maxConcurrentOperationCount set as 5. This approach might be helpful for you.

Kjuly commented 9 years ago

@renjithn good suggestion, but i think he got those images in a single response and tried to save them at same time.

@tarzon as the error said, it's busy saving images. I suggest u to copy those images into an array, once got one image saved succeed, save next one; but if failed, put the failed one to the end of array. And throw an error if failed to save a same image several times.

tarzon commented 9 years ago

Hi @Kjuly I will implement the algorithm for saving multiple images and let you know. @renjithn I tried your soultion and still got the write busy error quite often. I even set the maxConcurrentOperationCount to 1, but that also didn't help.

tarzon commented 9 years ago

Hi @Kjuly I just implemented the logic that you suggested and it worked very well. Thanks. The only thing that i have a doubt about is that it takes a lot of time to write the images to the Photo Library. Apart from that its working fine with average amount of memory being used.

Kjuly commented 9 years ago

Cool! Yeah, it'll take long time by this way, u can just leave it as a background job, let user do what they want on UI level. And I'll take time to see whether apple supports batch images saving. :)

tarzon commented 9 years ago

Thanks. :)