Reading the Photo Library in iOS development

Source: Internet
Author: User

In iOS, if we only need to read an image or a video or take a photo or video at a time, we can use UIImagePickerController. However, many times we need to read multiple photos or videos from PhotoLibrary at a time. At this time, we need to find another way. Fortunately, apple provides us with corresponding interfaces.

Before starting coding, we want to know several classes:

ALAssetsLibrary: indicates the entire PhotoLibrary. We can generate an Instance Object of the Image Library, which is equivalent to the handle of the Photo Library.

ALAssetsGroup: the group of the Photo Library. We can use the ALAssetsLibrary instance to obtain the handles of all the groups.

ALAsset: An ALAsset instance represents an asset, that is, a photo or video. We can obtain the corresponding subnail or source image through its instance.

One thing you need to know is blocks. apple has seen a large number of such things since iOS 4.0, which is more and more widely used, but it is really useful. I will not talk much about this stuff here. I will elaborate on my blog.

For the requirements in this article, we read the group and each asset asynchronously, but now we can use blocks in a function. So blocks is really convenient.

See the code below:

 
 
  1. ALAssetsLibrary * assetsLibrary = [[ALAssetsLibrary alloc] init]; // generates an instance of the entire photolibrary handle
  2. NSMutableArray * mediaArray = [[NSMutableArray alloc] init]; // stores the media Array
  3. [AssetsLibrary enumerateGroupsWithTypes: ALAssetsGroupAll usingBlock: ^ (ALAssetsGroup * group, BOOL * stop) {// obtain all Groups
  4. [Group enumerateAssetsUsingBlock: ^ (ALAsset * result, NSUInteger index, BOOL * stop) {// from the group
  5. NSString * assetType = [result valueForProperty: ALAssetPropertyType];
  6. If ([assetType isinclutostring: ALAssetTypePhoto]) {
  7. NSLog (@ "Photo ");
  8. } Else if ([assetType isinclutostring: ALAssetTypeVideo]) {
  9. NSLog (@ "Video ");
  10. } Else if ([assetType isinclutostring: ALAssetTypeUnknown]) {
  11. NSLog (@ "Unknow AssetType ");
  12. }
  13.  
  14. NSDictionary * assetUrls = [result valueForProperty: ALAssetPropertyURLs];
  15. NSUInteger assetCounter = 0;
  16. For (NSString * assetURLKey in assetUrls ){
  17. NSLog (@ "Asset URL % lu = % @", (unsigned long) assetCounter, [assetUrls objectForKey: assetURLKey]);
  18. }
  19.  
  20. NSLog (@ "Representation Size = % lld", [[result defaultRepresentation] size]);
  21. }];
  22. } FailureBlock: ^ (NSError * error ){
  23. NSLog (@ "Enumerate the asset groups failed .");
  24. }];

The rest is how to obtain the corresponding subnail or source image or other information from each asset. It's very easy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.