简体   繁体   English

将NSUrl转换为PHAsset iOS 8

[英]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. 我有NSUrl格式的图片列表,我想将其转换为对象的PHAsset列表。

How can I perform it? 我该如何执行?

My goal is to upload a list of pictures to my view on viewDidLoad state. 我的目标是将照片列表上传到viewDidLoad状态下的视图中。

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. 我开始使用iOS 8及更高版本中的PHAssets库,并且注意到从视图控制器退出时,没有一种简单的方法可以将所有资产保存在内存中。 Therefore what I did is a little conversion, i'm saving the PHAssets url as NSString object using NSUserDefaults. 因此,我做了一点转换,就是使用NSUserDefaults将PHAssets网址另存为NSString对象。

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. 现在,我要做的就是在加载状态下重新加载PHAssets列表,以使所有选定的图片在用户视图中再次可用。

Have you tried looking into the fetchAssetsWithALAssetURLs(_:options:) ? 您是否尝试过研究fetchAssetsWithALAssetURLs(_:options:) You will in-turn need to look into ALAssets if you are unfamiliar with them. 如果您不熟悉ALAsset,则需要转而研究它们。 Of course keep in mind that the Asset Library is depreciated in iOS 9. 当然,请记住,资产库在iOS 9中已弃用。

After investigation, PHAsset inherits a localIdentifier property from PHObject. 经过调查,PHAsset从PHObject继承了localIdentifier属性。 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. 基本上,localIdentifier是一个唯一的字符串,用于永久标识对象,由于它是NSString对象,因此以后可以使用NSUserDefaults将其存储在内存中。

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: 当我要提取PHAsset数组时,我使用了以下几行:

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

Then I iterate on the assets result and upload all my PHAsses objects like a regular NSArray. 然后,对资产结果进行迭代,并像常规NSArray一样上载我的所有PHAsses对象。 Information about it can be found here 可以在这里找到有关它的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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