简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM