Avi Levin
Avi Levin

Reputation: 1868

Convert NSUrl to PHAsset iOS 8

I have a list of pictures in NSUrl format that I want to convert to PHAsset list of object.

How can I perform it?

My goal is to upload a list of pictures to my view on viewDidLoad state.

I started to work with the PHAssets library in iOS 8 and above and I notice that there isn't a easy way to save all the assets in memory when exiting from the view controller. Therefore what I did is a little conversion, i'm saving the PHAssets url as NSString object using NSUserDefaults.

Now all I need to perform is to load back the PHAssets list on the load state, to have all the selected pictures available once again on the user view.

Upvotes: 0

Views: 3119

Answers (2)

Avi Levin
Avi Levin

Reputation: 1868

After investigation, PHAsset inherits a localIdentifier property from PHObject. Basically the localIdentifier is a unique string that persistently identifies the object which can later be stored in memory using NSUserDefaults since it's an NSString object.

What i did is extract this property and saved it into memory like this:

NSMutableArray* pbjects = [[NSMutableArray alloc]init];
for(id key in keys)
{
   PHAsset *asset = [dic objectForKey:key];
   [pbjects addObject:asset.localIdentifier];
}

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];[defaults setObject:pbjects forKey:KEY_PICTURES_LIST];
[defaults synchronize];

When I wanted to extract the PHAsset array I used the following lines:

PHFetchResult* assets =[PHAsset fetchAssetsWithLocalIdentifiers:picsUrls options:nil];

Then I iterate on the assets result and upload all my PHAsses objects like a regular NSArray. Information about it can be found here

Upvotes: 1

DARKUNIT22
DARKUNIT22

Reputation: 76

Have you tried looking into the fetchAssetsWithALAssetURLs(_:options:)? You will in-turn need to look into ALAssets if you are unfamiliar with them. Of course keep in mind that the Asset Library is depreciated in iOS 9.

Upvotes: 1

Related Questions