Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to initialize MWPhoto objects with an NSURL representing an asset found in the Asset Library #64

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions MWPhotoBrowser/Classes/MWPhoto.h
Expand Up @@ -24,11 +24,13 @@
+ (MWPhoto *)photoWithImage:(UIImage *)image;
+ (MWPhoto *)photoWithFilePath:(NSString *)path;
+ (MWPhoto *)photoWithURL:(NSURL *)url;
+ (MWPhoto *)photoWithAssetURL:(NSURL *)url;

// Init
- (id)initWithImage:(UIImage *)image;
- (id)initWithFilePath:(NSString *)path;
- (id)initWithURL:(NSURL *)url;
- (id)initWithAssetURL:(NSURL *)url;

@end

46 changes: 44 additions & 2 deletions MWPhotoBrowser/Classes/MWPhoto.m
Expand Up @@ -8,14 +8,16 @@

#import "MWPhoto.h"
#import "MWPhotoBrowser.h"
#import <AssetsLibrary/AssetsLibrary.h>

// Private
@interface MWPhoto () {

// Image Sources
NSString *_photoPath;
NSURL *_photoURL;

NSURL *_assetURL;

// Image
UIImage *_underlyingImage;

Expand Down Expand Up @@ -55,6 +57,10 @@ + (MWPhoto *)photoWithURL:(NSURL *)url {
return [[[MWPhoto alloc] initWithURL:url] autorelease];
}

+ (MWPhoto *)photoWithAssetURL:(NSURL *)url {
return [[[MWPhoto alloc] initWithAssetURL:url] autorelease];
}

#pragma mark NSObject

- (id)initWithImage:(UIImage *)image {
Expand All @@ -78,6 +84,13 @@ - (id)initWithURL:(NSURL *)url {
return self;
}

- (id)initWithAssetURL:(NSURL *)url {
if ((self = [super init])) {
_assetURL = [url copy];
}
return self;
}

- (void)dealloc {
[_caption release];
[[SDWebImageManager sharedManager] cancelForDelegate:self];
Expand Down Expand Up @@ -107,7 +120,10 @@ - (void)loadUnderlyingImageAndNotify {
// Load async from web (using SDWebImage)
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:_photoURL delegate:self];
} else {
} else if (_assetURL) {
// Load async from asset library
[self performSelectorInBackground:@selector(loadImageFromAssetAsync) withObject:nil];
}else {
// Failed - no source
self.underlyingImage = nil;
[self imageLoadingComplete];
Expand Down Expand Up @@ -146,6 +162,32 @@ - (void)loadImageFromFileAsync {
}
}

// Called in background
// Load image in background from asset library
- (void)loadImageFromAssetAsync {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try {
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:_assetURL
resultBlock:^(ALAsset *asset){
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullScreenImage];
if (iref) {
self.underlyingImage = [UIImage imageWithCGImage:iref];
}
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}
failureBlock:^(NSError *error) {
self.underlyingImage = nil;
MWLog(@"Photo from file error: %@",error);
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}];
} @catch (NSException *exception) {
} @finally {
[pool drain];
}
}

// Called on main
- (void)imageDidFinishLoadingSoDecompress {
NSAssert([[NSThread currentThread] isMainThread], @"This method must be called on the main thread.");
Expand Down
7 changes: 5 additions & 2 deletions README.markdown
Expand Up @@ -15,7 +15,7 @@ MWPhotoBrowser is an implementation of a photo browser similar to the native Pho

## Usage

MWPhotoBrowser is designed to be presented within a navigation controller. Simply set the delegate (which must conform to `MWPhotoBrowserDelegate`) and implement the 2 required delegate methods to provide the photo browser with the data in the form of `MWPhoto` objects. You can create an `MWPhoto` object by providing a `UIImage` object, a file path to a physical image file, or a URL to an image online.
MWPhotoBrowser is designed to be presented within a navigation controller. Simply set the delegate (which must conform to `MWPhotoBrowserDelegate`) and implement the 2 required delegate methods to provide the photo browser with the data in the form of `MWPhoto` objects. You can create an `MWPhoto` object by providing a `UIImage` object, a file path to a physical image file, a URL to an image online, or a URL to an asset stored in the Assets Library.

`MWPhoto` objects handle caching, file management, downloading of web images, and various optimisations for you. If however you would like to use your own data model to represent photos you can simply ensure your model conforms to the `MWPhoto` protocol. You can then handle the management of caching, downloads, etc, yourself. More information on this can be found in `MWPhotoProtocol.h`.

Expand All @@ -27,6 +27,9 @@ See the code snippet below for an example of how to implement the photo browser.
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3629/3339128908_7aecabc34b.jpg"]]];
[photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3590/3329114220_5fbc5bc92b.jpg"]]];

// assetURL can be any NSURL pointing to an asset from the Assets Library
[photos addObject:[MWPhoto photoWithAssetURL:assetURL]];

// Create & present browser
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set options
Expand Down Expand Up @@ -141,4 +144,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.