iOS Question Help of ObjC expert required for 30 secs

JackKirk

Well-Known Member
Licensed User
Longtime User
I'm trying to add some functionality to Narek Adonts ALAssets wrapper:

https://www.b4x.com/android/forum/threads/alassets-wraps-the-native-alassets-framework-photos.65231/

I want to get the SaveBitmapToAlbum objective C to return the internal asset object it generates.

If I do this then I will be able to maintain an external "photo name" vs "asset object" table and hence have much greater control over the albums etc.

The following is a slightly modified form of the original which compiles OK:
B4X:
-(void)SaveBitmapToAlbum :(ALAssetsGroup*)group :(UIImage*)image
{

CGImageRef img = [image CGImage];
ALAssetsLibrary *library = [self defaultAssetsLibrary] ;
[library writeImageToSavedPhotosAlbum:img
                                  metadata:nil
                           completionBlock:^(NSURL* assetURL, NSError* error) {
     if (error.code == 0) {
         NSLog(@"saved image completed:\nurl: %@", assetURL);

         // try to get the asset
         ALAssetsLibrary *library = [self defaultAssetsLibrary] ;
         [library assetForURL:assetURL
                       resultBlock:^(ALAsset* asset) {
              // assign the photo to the album
              [group addAsset:asset];
              NSLog(@"Added %@", asset);
          }
                      failureBlock:^(NSError* error) {
              NSLog(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
          }];
     }
     else {
         NSLog(@"saved image failed.\nerror code %i\n%@", error.code, [error localizedDescription]);
     }
}];
}

and the NSLog(@"Added %@", asset); line is logging what I am trying to extract.

My attempt at modifying the code is:
B4X:
//jk
-(ALAsset*)SaveBitmapToAlbum :(ALAssetsGroup*)group :(UIImage*)image
{

CGImageRef img = [image CGImage];
ALAssetsLibrary *library = [self defaultAssetsLibrary] ;
[library writeImageToSavedPhotosAlbum:img
                                  metadata:nil
                           completionBlock:^(NSURL* assetURL, NSError* error) {
     if (error.code == 0) {
         NSLog(@"saved image completed:\nurl: %@", assetURL);

         // try to get the asset
         ALAssetsLibrary *library = [self defaultAssetsLibrary] ;
         [library assetForURL:assetURL
                       resultBlock:^(ALAsset* asset) {
              // assign the photo to the album
              [group addAsset:asset];
              NSLog(@"Added %@", asset);

//jk
return asset;

          }
                      failureBlock:^(NSError* error) {
              NSLog(@"failed to retrieve image asset:\nError: %@ ", [error localizedDescription]);
          }];
     }
     else {
         NSLog(@"saved image failed.\nerror code %i\n%@", error.code, [error localizedDescription]);
     }
}];

//jk
return nil;

}

I have only changed 3 lines, all with the //jk comment.

This bombs at the line:

resultBlock:^(ALAsset* asset) {

with this error message:

/Users/administrator/Documents/UploadedProjects/<user id>/B4iProject/b4i_alassets.m:388:36: error: incompatible block pointer types sending 'ALAsset *(^)(ALAsset *__strong)' to parameter of type 'ALAssetsLibraryAssetForURLResultBlock' (aka 'void (^)(ALAsset *__strong)')
resultBlock:^(ALAsset* asset) {
^~~~~~~~~~~~~~~~~~~

and several other warnings.

I suspect I am doing something very stupid - all help appreciated.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Thanks Erel, that was enough to get me over the hump.

One question - I have googled myself stupid trying to find a way to determine what photos are associated with a given album using ALAssets.

Do you know if it is possible or do you have to go to PHAsset?
 
Upvote 0
Top