Saving Your Video And Photo Assets To A Custom Album iOS

Hello, welcome to my 2nd tutorial. How to save your video and photo assets to a custom album in iOS using Objective-C. If you are following my blogs, you already know that I am blogging everyday about my work, my projects, basically my adventure in iOS Software Development.

So let's begin this tutorial. During the making of my main project, I learned by myself how to save an asset to a custom album. By default, taking a video and photo using the iOS ImagePicker Camera Source will let you automatically save your asset to Camera Roll. Without anymore further ado, let's begin.

I am using the ALAssetsLibrary+CustomPhotoAlbum library by Martin Todorov. See the library's GitHub Repository by clicking the link. I tried my best to study the library (well, I didn't really study it, just did trial and error), and after few hours, I finally got to save my assets to my custom album!

Ohhh, I'm afraid this is not the tutorial you are looking for, I'm just going to show you what Martin Todorov has written in his Github repository. In order to use this library, you will need to catch or get the url of your capture Video or Photo. It's pretty easy you know, once you get yourself familiar with objective-c.

Here we go, here are the instructions and codes to save your assets:

SAVING PHOTO ASSET:

To save your image, call this method:

- (void)saveImage:(UIImage *)image
          toAlbum:(NSString *)albumName
       completion:(ALAssetsLibraryWriteImageCompletionBlock)completion
          failure:(ALAssetsLibraryAccessFailureBlock)failure;

Parameters of saveImage Method:
//   |videoUrl|: The target video to be saved
//  |albumName|: Custom album name
// |completion|: Block to be executed when succeed to write the image data
//               to the assets library (camera roll)
//    |failure|: Block to be executed when failed to add the asset to the
//               custom photo album



SAVING VIDEO ASSET:

Call this method to save your video.

- (void)saveVideo:(NSURL *)videoUrl
          toAlbum:(NSString *)albumName
       completion:(ALAssetsLibraryWriteImageCompletionBlock)completion
          failure:(ALAssetsLibraryAccessFailureBlock)failure;

Parameters of saveVideo Method:
//   |videoUrl|: The target video to be saved
//  |albumName|: Custom album name
// |completion|: Block to be executed when succeed to write the image data
//               to the assets library (camera roll)
//    |failure|: Block to be executed when failed to add the asset to the
//               custom photo album

Look, I just found out that this super cool, awesome library has also the power to save METADATA to your image asset! WOW!


WRITING THE IMAGE ASSET TO ASSETS LIBRARY (CAMERA ROLL) WITH META DATA:

Call this method to save your image with meta data.

- (void)saveImageData:(NSData *)imageData
              toAlbum:(NSString *)albumName
             metadata:(NSDictionary *)metadata
           completion:(ALAssetsLibraryWriteImageCompletionBlock)completion
              failure:(ALAssetsLibraryAccessFailureBlock)failure;

Parameters of saveVideo Method:
//  |imageData|: The image data to be saved
//  |albumName|: Custom album name
//   |metadata|: Meta data for image
// |completion|: Block to be executed when succeed to write the image data
//    |failure|: block to be executed when failed to add the asset to the custom photo album

UPDATE: I learned that you need to initiate/instantiate this library before you can use it.

Post a Comment