1

Hi I am developing an App based on chatting now I want to save images locally for that I have created a directory "My_Videos" in locally in device using below code.

 NSString *albumName=@"My_Videos";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:albumName
                         resultBlock:^(ALAssetsGroup *group) {
                             NSLog(@"added album:%@", albumName);
                         }
                        failureBlock:^(NSError *error) {
                            NSLog(@"error adding album");
                        }];

It is creating folder successfully But now I want to download images from Web API's and save in "My_Videos" folder which I have created in device. Please help me out.

Bittoo
  • 579
  • 7
  • 21
  • 1
    i guess you can save only in your document directory, correct me if I am wrong – Syed Ali Salman Feb 04 '16 at 12:44
  • yes But i want to save in specified floder – Bittoo Feb 04 '16 at 13:08
  • You can save an image/video in to your Camera Roll Album. I believe there is no way to select a specific folder in your Photos Album. Check this out: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/index.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum – iOSAddicted Feb 04 '16 at 13:27
  • @iOSAddicted Thanks for the replay but some apps have this functionality right? so i am trying if you know any thing else please let me know – Bittoo Feb 04 '16 at 13:33
  • @iosAddicted please Accept this as question it will use full some body else if you think it will use full – Bittoo Feb 06 '16 at 14:22

1 Answers1

1

You were on the right track to create a new group in AssetsLibrary. Here is how you can add the photo there :

First you need to write the photo to Standard Assets Library (Camera Roll)

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum:image.CGImage
                          orientation:(ALAssetOrientation)image.imageOrientation
                      completionBlock:^(NSURL* assetURL, NSError* error)
 {
     if (!error) {

         __block BOOL albumAlredyCreated = NO;

         // Check if Album already exists
         [library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                                usingBlock:^(ALAssetsGroup *group, BOOL *stop)
          {
              if ([customAlbum compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame)
              {
                  albumAlredyCreated = YES;

                  //Get the ALAsset instance for the saved image
                  [library assetForURL: assetURL
                           resultBlock:^(ALAsset *asset)
                   {

                       //Save photo to customAlbum
                       [group addAsset: asset];

                   } failureBlock:^(NSError *error) {

                       // If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
                       // If the data is currently unavailable, the failure block will be called.
                   }];
              }

              // Album does not exist. Create it
              if (group == nil && albumAlredyCreated == NO)
              {
                  __weak ALAssetsLibrary* weakLibrary = library;

                  [library addAssetsGroupAlbumWithName:customAlbum
                                           resultBlock:^(ALAssetsGroup *group)
                   {

                       //Get the ALAsset instance for the saved image
                       [weakLibrary assetForURL: assetURL
                                    resultBlock:^(ALAsset *asset) {

                                        //Save photo to customAlbum
                                        [group addAsset: asset];

                                    } failureBlock:^(NSError *error) {

                                        // If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
                                        // If the data is currently unavailable, the failure block will be called.
                                    }];
                   } failureBlock:^(NSError *error) {

                       // If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
                   }];
              }

          } failureBlock:^(NSError *error) {

              // If the user denies access to the application, or if no application is allowed to access the data, the failureBlock is called.
          }];
     }
 }];

Although this would work but ALAssetsLibrary is depricated in iOS 9, so you may want to consider moving to PHPhotoLibrary.

NS_CLASS_DEPRECATED_IOS(4_0, 9_0, "Use PHPhotoLibrary from the Photos framework instead") @interface ALAssetsLibrary : NSObject

UditS
  • 1,936
  • 17
  • 37
  • Thanks for the replay but I want only save the images till now can you please tell me how to do that? – Bittoo Feb 05 '16 at 16:02
  • Thanks for the replay i am getting this error "Connection to assetsd was interrupted or assetsd died" where i miss? – Bittoo Feb 06 '16 at 08:38
  • Try Resetting the simulator. This question is already asked [here](http://stackoverflow.com/questions/26658934/connection-to-assetsd-was-interrupted-or-assetsd-died) – UditS Feb 06 '16 at 09:56