Skip to content

Instantly share code, notes, and snippets.

@bnorton
Last active August 29, 2015 14:02
Show Gist options
  • Save bnorton/17594378fced77f355d4 to your computer and use it in GitHub Desktop.
Save bnorton/17594378fced77f355d4 to your computer and use it in GitHub Desktop.
Some helpful ALAssetsLibrary helpers.
//
// ALAssetsLibrary+CoreExt.h
//
// Created by Brian Norton on 6/14/14.
//
@interface ALAssetsLibrary (CoreExt)
+(ALAssetsLibrary *)library;
-(BOOL)canRequestAccess;
-(BOOL)authorized;
-(ALAsset *)assetNamed:(id)name;

####Photos Permission This will make sure your request for photos is ~95% certain of being approved.

By knowing that you have not asked for access yet [ALAssetsLibrary library].canRequestAccess you can ask the user if they will allow access to photos if you ask. With the addition of a blocking way to load a photo (if you're already in an NSOperation or a GCD block) you'll have all the tools you need.

If they say yes to the cheap question (will you allow access?), you can then make the expensive question (you can only ask once).

//
// ALAssetsLibrary+CoreExt.m
//
// Created by Brian Norton on 6/14/14.
//
#import "ALAssetsLibrary+CoreExt.h"
@implementation ALAssetsLibrary (CoreExt)
+(ALAssetsLibrary *)library { static ALAssetsLibrary* library;
if(!library)
library = [[ALAssetsLibrary alloc] init];
return library;
}
-(BOOL)canRequestAccess {
return [ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusNotDetermined;
}
-(BOOL)authorized {
return [ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized;
}
-(ALAsset *)assetNamed:(id)name {
NSURL *url = [name isKindOfClass:[NSURL class]] ?
name : [NSURL URLWithString:[NSString stringWithFormat:@"%@", name]];
__block ALAsset *asset; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[[self.class library] assetForURL:url resultBlock:^(ALAsset *incoming) {
asset = incoming;
dispatch_semaphore_signal(semaphore);
} failureBlock:^(NSError *error) {
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return asset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment