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

Noob question. #21

Closed ghost closed 9 years ago

ghost commented 9 years ago

While I used this library flawlessly on my previous builds, iOS 8 has given me trouble as mentioned in a few other issues, yet I can't grasp it.

My question is posted here: https://stackoverflow.com/questions/27585750/alassetslibrary-customphotoalbum-ios-8

Kjuly commented 9 years ago

Hi @ChrisOSX , thx for ur feedback. I just take a look at ur question, but sorry i'm not very clear about it. B.t.w., have u installed the latest version (1.2.0) of this lib?

ghost commented 9 years ago

When you download the zip from the main page, the version in the project is 1.1.3, is there a newer version?

When I first started using your lib, calling this below would function in saving and creating the folder i named: [self.library saveImage:image toAlbum:@"" withCompletionBlock:^(NSError *error) { if (error!=nil) { NSLog(@"Big error: %@", [error description]); } }];

As mentioned in a couple other issues about recreating deleted folders on ios 8 there was a fix in the lib. I unfortunately do not have much experience using blocks and for the life of me figure out the completion and failure portion. [self.library saveImage:image toAlbum:@"insanelyi" completion:?? failure:?? { }];

Kjuly commented 9 years ago

When you download the zip from the main page, the version in the project is 1.1.3, is there a newer version?

Yes, the demo project's version is v1.1.3, should be right. it's

As mentioned in a couple other issues about recreating deleted folders on ios 8 there was a fix in the lib. I unfortunately do not have much experience using blocks and for the life of me figure out the completion and failure portion. [self.library saveImage:image toAlbum:@"insanelyi" completion:?? failure:?? { }];

Yeah, the latest lib uses -saveImage:toAlbum:completion:failure: now, and there's a demo project in the lib, u can take a look at it:

...
    // The completion block to be executed after image taking action process done
    void (^completion)(NSURL *, NSError *) = ^(NSURL *assetURL, NSError *error) {
      if (error) NSLog(@"!!!ERROR,  write the image data to the assets library (camera roll): %@",
                       [error description]);
      NSLog(@"*** URL %@ | %@ || type: %@ ***", assetURL, [assetURL absoluteString], [assetURL class]);
      // Add new item to |photos_| & table view appropriately
      NSIndexPath * indexPath = [NSIndexPath indexPathForRow:self.photos.count
                                                   inSection:0];
      [self.photos addObject:[assetURL absoluteString]];
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView insertRowsAtIndexPaths:@[indexPath]
                              withRowAnimation:UITableViewRowAnimationFade];
      });
    };

    void (^failure)(NSError *) = ^(NSError *error) {
      if (error == nil) return;
      NSLog(@"!!!ERROR, failed to add the asset to the custom photo album: %@", [error description]);
    };

    // Save image to custom photo album
    // The lifetimes of objects you get back from a library instance are tied to
    //   the lifetime of the library instance.
    [self.assetsLibrary saveImage:finalImageToSave
                          toAlbum:kKYCustomPhotoAlbumName_
                       completion:completion
                          failure:failure];
...
ghost commented 9 years ago

Do you see anything wrong in how I'm calling it here? I'm sending the image to self.library saveImage:image, so I don't think i need to declare anything else in the completion block.

    NSURL *imageURL = receivedURL;
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
    NSString *albumName = @"folder";

    // The completion block to be executed after image taking action process done
    void (^completion)(NSURL *, NSError *) = ^(NSURL *assetURL, NSError *error) {

        if (error) NSLog(@"!!!ERROR,  write the image data to the assets library (camera roll): %@",
                         [error description]);
        NSLog(@"*** URL %@ | %@ || type: %@ ***", assetURL, image, [assetURL class]);

    };

    void (^failure)(NSError *) = ^(NSError *error) {
        if (error == nil) return;
        NSLog(@"!!!ERROR, failed to add the asset to the custom photo album: %@", [error description]);
    };

    [self.library saveImage:image
                          toAlbum:albumName
                       completion:completion
                          failure:failure];
Kjuly commented 9 years ago

Seems okay, but need to confirm with u about two issues below:

  1. Have ur image loaded succeed before u save it to the album? If not, maybe u'd better to use some lib (like SDWebImage or AFNetworking's image category), there's a completion block to be invoked after loading the image succeed.
  2. Have ur self.library initialized before u use it? If not, u need to alloc an instance for it.
ghost commented 9 years ago

Alright if you think it's ok. I just didn't know if I needed to add anything else.

  1. Yes the image is fully loaded. It gets loaded in a image view controller, then button to action sheet then save image which fires your lib. So everything is in place before the save image gets called.
  2. In my viewDidLoad I have self.library = [[ALAssetsLibrary alloc] init];. So it's getting initialized when the image view controller get's called.

I only brought it up to begin with because a tester I have said it wouldn't recreate the folder.

Kjuly commented 9 years ago

Yeah, if those two are right, I guess it should works now. Take a try, and u might need to update the ui to let user know that the image saved succeed. :)

ghost commented 9 years ago

Sweet :) Yah I have a nice popup letting users know it's been downloaded. I appreciate all of the help. If everything looks good to you, then we can close this issue for now.

Kjuly commented 9 years ago

Sure, let me know if u still got the problem to let it work. Thx :)