Ios-Get all photos in the system album, ios-get album photos

Source: Internet
Author: User

Ios-Get all photos in the system album, ios-get album photos

# 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 url of the image

/* 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.