In iOS, if we only need to read an image or a video (or take a photo or video) at one 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:
Alassetslibrary * assetslibrary = [[alassetslibrary alloc] init]; // The instance that generates the entire photolibrary handle nsmutablearray * mediaarray = [[nsmutablearray alloc] init]; // storage media array [assetslibrary failed: alassetsgroupall usingblock: ^ (alassetsgroup * group, bool * Stop) {// obtain all groups [Group enumerateassetsusingblock: ^ (alasset * result, nsuinteger index, bool * Stop) {// nsstring * assettype = [result valueforproperty: alassetpropertytype] from the group; If ([assettype isinclutostring: alassettypephoto]) {nslog (@ "photo");} else if ([assettype isinclutostring: alassettypevideo]) {nslog (@ "video");} else if ([assettype isinclutostring: alassettypeunknown]) {nslog (@ "unknow assettype");} nsdictionary * asseturls = [result valueforproperty: alassetpropertyurls]; nsuinteger assetcounter = 0; For (nsstring * asseturlkey in asseturls) {nslog (@ "asset URL % lu = % @", (unsigned long) assetcounter, [asseturls objectforkey: asseturlkey]);} nslog (@ "representation size = % LLD ", [[result defaultrepresentation] size]) ;}] ;}failureblock: ^ (nserror * error) {nslog (@ "enumerate the asset groups failed. ") ;}];
The rest is how to obtain the corresponding thumbnail or source image or other information from each asset. It's very easy.
It's too late. Go to bed first and talk about it tomorrow. Please pay attention to the next article.