Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The documentation of NSArray for the "writeToFile:atomically:" method, shows that all members must be property list objects. ALAsset is not a property list object, so writing that to a file is not going to work. </p> <blockquote> <p>I know that its a ALAsset object, in the AGImagePickerController it is compared as NSDictionary</p> </blockquote> <p>If you looked carefully then you would have seen that it does not compare ALAsset's, but their 'ALAssetPropertyURLs' property. The value of that property is an NSDictionary.</p> <p>As ALAsset does not have a public constructor, there is no way you can reconstruct it after reading from a file or NSUserDefaults, even if you manage to write it.</p> <p>So the best thing you can do is to re-fetch the ALAssets from the source that you originally got them from. I assume that is an ALAssetsGroup? Instead of saving to file and retrieving again, why don't you just regenerate them with the same query on ALAssetsGroup as you originally used to generate them?</p> <p><strong>EDIT</strong>:</p> <p>So you say you got the original ALAsset's from an AGImagePickerController. In order to store them, you can take Matej's advice in the comments and store the URLs that identify them.</p> <p>But keep in mind that AGImagePickerController is a means for the user to pick a number of photos and then <strong><em>do something with them</em></strong>. That is, the ALAssets are simply intermediare results pointing to the original locations of the photos. If you store the URL's and retrieve them later, there is no guarantee at all that the originals are still there.</p> <p>So ask yourself: what is it that you want the user to do with the photos, and store the result of that action, rather than the assets themselves. For example, one reasonable action you could do is to create a new ALAssetGroup (with the addAssetsGroupAlbumWithName: method on ALAssetsLibrary), and store the assets in there. ALAssetGroups are automatically saved, so you don't need to do anything yourself for that.</p> <p><strong>EDIT 2</strong> - after more information from the OP</p> <p>What Matej hints at in the comments, is to convert the array of ALAssets that you have into an array of dictionaries by retrieving the urls from the assets. As you can read in the <a href="http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAsset_Class/Reference/Reference.html" rel="nofollow">ALAsset class documentation</a> you can do that in the following way:</p> <pre><code>NSArray *assetArray = // your array of ALAssets NSMutableArray *urls = [NSMutableArray arrayWithCapacity:assetArray.count]; for( ALAsset *asset in assetArray ) { NSDictionary *urlDictionary = [asset valueForProperty:@"ALAssetPropertyURLs"]; [urls addObject:urlDictionary]; } </code></pre> <p>The resulting array of dictionaries you can save in any way you like.</p> <p>After restart of your app, you read the array of dictionaries back from where you stored it. Then Matej suggests to use ALAssetsLibrary's <code>assetForURL:resultBlock:failureBlock:</code> to recreate the ALAssets. But as we now know you want to put a checkmark on the original assets again, it is better to fetch the original array of ALAssets, and check whether any of them are present in the recovered urls. The following should work for that:</p> <pre><code>NSArray *assetArray = // the full array of ALAssets from AGImagePickerController NSArray *urls = // the recovered array of NSDictionaries for( ALAsset *asset in assetArray ) { NSDictionary *urlDictionary = [asset valueForProperty:@"ALAssetPropertyURLs"]; if( [urls containsObject:urlDictionary] ) { ... // set checkmark on asset } } </code></pre> <p>This assumes the original assets have not changed, which is not under your control (the user has removed/added photos, for example).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload