DrDoom
DrDoom

Reputation: 149

PHPhotoLibrary getting album and photo info

I am trying to get info on all the albums/photos using the PHPhotoLibrary. I barely know objective C, and i've looked at some tutorial/sample but couldn't find everything that I needed.

Here is a link to the sample code I based my code on. https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html#//apple_ref/doc/uid/TP40014575-Intro-DontLinkElementID_2

So far I was able to get the albums name and identifier. And I am getting a list of photos, I am able to get their identifier as well, but not the filename. But if I put a break point in my fonction and look at my PHAsset pointer values, I can see the filename there (inside _filename), but if I try to call the variable with the filename in it, the variable does not exist.

So if anyone can provide a sample code to get all info on albums/photos/thumbnail that would be awesome. Or just getting the filename would be a good help.

Here is the code I have tried so far:

-(void)awakeFromNib{
    NSMutableArray *allPhotos = self.getAllPhotos;
    for (int x = 0; x < allPhotos.count; x ++)
    {
        PHAsset *photo = [self getPhotoAtIndex:x];
        PHAssetSourceType source = photo.sourceType;
        NSString *id = photo.localIdentifier;
        NSString *description = photo.description;
        NSUInteger height = photo.pixelHeight;
        NSUInteger width = photo.pixelWidth;
        NSLog(@"Test photo info");
    }
}

-(PHAsset*) getPhotoAtIndex:(NSInteger) index
{
    return [self.getAllPhotos objectAtIndex:index];
}

-(NSMutableArray *) getAllPhotos
{
    NSMutableArray *photos = [[NSMutableArray alloc] init];
    PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
    allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
    PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
    PHFetchResult *fetchResult = @[allPhotos][0];

    for (int x = 0; x < fetchResult.count; x ++) {

        PHAsset *asset = fetchResult[x];
        photos[x] = asset;
    }

    return photos;
}

As you can see, I can get the image height and width, its id, but cannot get the url to it.

Upvotes: 2

Views: 5217

Answers (2)

rickster
rickster

Reputation: 126127

Filenames in the Photos library are an implementation detail and subject to change. There are various private API for discovering them (or ways to use valueForKey or other public introspection APIs to find where they're hidden), they aren't something to be relied upon. In particular, an asset that's been edited is likely to have a different filename than the original.

What do you need a filename/URL for? If you're just uniquely identifying the asset across launches of your app, use localIdentifier. If you're showing it to the user... why? Something like IMG_0234.jpg vs IMG_5672.jpg has little meaning to the average user.


To fetch the assets in a specific album, use fetchAssetsInAssetCollection:options:. To fetch the album(s) containing a specific asset, use fetchAssetCollectionsContainingAsset:withType:options:. To discover the list(s) of albums, use other APIs on PHAssetCollection and its superclass PHCollection.

Upvotes: 0

DrDoom
DrDoom

Reputation: 149

I have found a way to get the url of my photo.

-(void)getImageURL:(PHAsset*) asset
{    
    PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
    [options setCanHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) {
        return [adjustmentData.formatIdentifier isEqualToString:AdjustmentFormatIdentifier] && [adjustmentData.formatVersion isEqualToString:@"1.0"];
    }];
    [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info)
    {
        NSURL* url = contentEditingInput.fullSizeImageURL;
    }];
}

Upvotes: 1

Related Questions