Obtain thumbnails of all photos and photos in an album

Source: Internet
Author: User



-(Void) loadAssets {

// Initialise
_ Assets = [NSMutableArray new];
_ AssetLibrary = [[ALAssetsLibrary alloc] init];

// Run in the background as it takes a while to get all assets from the library
Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {

NSMutableArray * assetGroups = [[NSMutableArray alloc] init];
NSMutableArray * assetURLDictionaries = [[NSMutableArray alloc] init];

// Process assets
Void (^ assetEnumerator) (ALAsset *, NSUInteger, BOOL *) = ^ (ALAsset * result, NSUInteger index, BOOL * stop ){
If (result! = Nil ){
If ([[result valueForProperty: ALAssetPropertyType] isEqualToString: ALAssetTypePhoto]) {
[AssetURLDictionaries addObject: [result valueForProperty: ALAssetPropertyURLs];
NSURL * url = result. defaultRepresentation. url;
[_ AssetLibrary assetForURL: url
ResultBlock: ^ (ALAsset * asset ){
If (asset ){
@ Synchronized (_ assets ){
[_ Assets addObject: asset];
If (_ assets. count = 1 ){
// Added first asset so reload data
// [Self. tableView into mselecw.mainthread: @ selector (reloadData) withObject: nil waitUntilDone: NO];
}
}
}

}
FailureBlock: ^ (NSError * error ){
NSLog (@ "operation was not successfull! ");
}];

}
}
};


ALAssetsLibraryAccessFailureBlock failureblock =
^ (NSError * myerror)
{
NSLog (@ "album Access failed =%@", [myerror localizedDescription]);
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 .");
}
};


// Process groups
Void (^ assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^ (ALAssetsGroup * group, BOOL * stop ){
If (group! = Nil ){
NSString * g = [NSString stringWithFormat: @ "% @", group]; // gets the album group.
NSString * g1 = [g substringFromIndex: 16];
NSArray * arr = [[NSArray alloc] init];
Arr = [g1 componentsSeparatedByString: @ ","];
NSString * g2 = [[arr objectAtIndex: 0] substringFromIndex: 5];
If ([g2 isEqualToString: @ "Camera Roll"]) {
G2 = @ "camera film ";
}
If ([g2 isEqualToString: @ "MyPhoto"]) {
* Stop = NO;
[Group enumerateassetswitexceptions: NSEnumerationReverse usingBlock: assetEnumerator];
[AssetGroups addObject: group];
}

} Else {
NSLog (@ "********* else! ");
NSMutableArray * photos = [[NSMutableArray alloc] init];
NSMutableArray * thumbs = [[NSMutableArray alloc] init];
@ Synchronized (_ assets ){
NSMutableArray * copy = [_ assets copy];
For (ALAsset * asset in copy ){
[Photos addObject: [MWPhoto photoWithURL: asset. defaultRepresentation. url];
[Thumbs addObject: [MWPhoto photoWithImage: [UIImage imageWithCGImage: asset. thumbnail];
}
}
Self. photos = photos; // photo
Self. thumbs = thumbs; // thumbnail
}
};

// Process!
[Self. assetLibrary enumerateGroupsWithTypes: ALAssetsGroupAll
UsingBlock: assetGroupEnumerator
FailureBlock: failureblock];
});

}


Technical exchange field, job search->

Welcome to the Public Account: iOSDevTip [scan the following QR code directly]


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.