IOS-Get all photos in the system album

Source: Internet
Author: User

# Import <assetslibrary/assetslibrary. h>

-(Void) getimgs {

Dispatch_async (dispatch_get_main_queue (), ^ {

NSAID utoreleasepool * Pool = [[NSAID utoreleasepoolalloc]
Init];

Alassetslibraryaccessfailureblock failureblock = ^ (nserror * myerror ){


Nslog (@ "album Access failed =%@", [myerrorlocalizeddescription]);


If ([myerror. localizeddescription
Rangeofstring: @ "Global denied access"]. location! = Nsnotfound ){

Nslog (@ "unable to access the album. Please go to 'settings-> locate service' and set it to open .");

} Else {

Nslog (@ "album Access failed .");

}

};


Alassetsgroupenumerationresultsblock groupenumeration = ^ (alasset * result, nsuinteger index,
Bool * Stop ){


If (result! = NULL ){


If ([[result valueforproperty: alassetpropertytype] isequaltostring: alassettypephoto]) {


Nsstring * urlstr = [nsstring
Stringwithformat: @ "% @", result. defaultrepresentation. url]; // The image URL

/* Result. defaultrepresentation. fullscreenimage // large image

Result. thumbnail // thumbnail of the image

// Nsange range1 = [urlstr rangeofstring: @ "id ="];

// Nsstring * resultname = [urlstr substringfromindex: range1.location + 3];

// Resultname = [resultname stringbyreplacingoccurrencesofstring: @ "& ext =" withstring: @ "."]; // format Demo: 123456.png

*/

[Self. _ dataarray addobject: urlstr];

}

}

};

Alassetslibrarygroupsenumerationresultsblock

Librarygroupsenumeration = ^ (alassetsgroup * group, bool * Stop ){


If (group = nil)

{

}


If (group! = Nil ){


Nsstring * g = [nsstring
Stringwithformat: @ "% @", group]; // gets the album Group

Nslog (@ "GG: % @", g );//GG: alassetsgroup
-Name: camera roll, type: saved photos, assets count: 71


Nsstring * G1 = [g substringfromindex: 16];


Nsarray * arr = [nsarray arraywitharray: [g1componentsseparatedbystring: @ ","];


Nsstring * g2 = [[arr objectatindex: 0] substringfromindex: 5];


If ([G2 isequaltostring: @ "camera roll"]) {

G2 = @ "camera film ";

}


Nsstring * groupname = G2; // group name

[Groupenumerateassetsusingblock: groupenumeration];

}

};

Alassetslibrary * library = [[alassetslibraryalloc]
Init];

[Libraryenumerategroupswithtypes: alassetsgroupall

Usingblock: librarygroupsenumeration

Failureblock: failureblock];

[Library
Release];

[Pool
Release];

});

}

// ------------------------ Reverse image Extraction Based on the image URL -----

Alassetslibrary * assetlibrary = [[alassetslibraryalloc]
Init];

Nsurl * url = [nsurlurlwithstring: urlstr];

[Assetlibrary
Assetforurl: URL resultblock: ^ (alasset * asset ){


Uiimage * image = [uiimage
Imagewithcgimage: asset. thumbnail];

Cellimageview. Image = image;

} Failureblock: ^ (nserror * error ){


Nslog (@ "error = % @", error );

}

];

//---------------------

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.