Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

3 participants

@arunisrael

Thanks for this cool photo gallery! Let me know if you have any feedback on the changes.

@mwaterfall
Owner

Thanks for the pull request. I'll check it out :-)

@runemalm

What's the status of this pull request?
I found myself in need of displaying photos from asset URLs. I applied the patch and can confirm that it works nicely. Does the loading off main thread as well..
Thanks arunisrael

@mwaterfall mwaterfall referenced this pull request from a commit
@mwaterfall MWPhotos initialised with URLs can now handle, file, asset or web pat…
…hs. Requires AssetsLibrary.framework.

As discussed in #64
cb30c15
@mwaterfall
Owner

Hey guys, I've implemented this on the develop branch and I'd be really grateful if you could test it out and make sure it's loading things from the asset library as you would expect. If all works okay I'll merge this into master.

@mwaterfall
Owner

This is now tested and merged into master. The demo app loads photos from the asset library for testing too!

@mwaterfall mwaterfall closed this
@gongpengjun gongpengjun referenced this pull request from a commit
@mwaterfall MWPhotos initialised with URLs can now handle, file, asset or web pat…
…hs. Requires AssetsLibrary.framework.

As discussed in #64
1a77c65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  MWPhotoBrowser/Classes/MWPhoto.h
@@ -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
View
46 MWPhotoBrowser/Classes/MWPhoto.m
@@ -8,6 +8,7 @@
#import "MWPhoto.h"
#import "MWPhotoBrowser.h"
+#import <AssetsLibrary/AssetsLibrary.h>
// Private
@interface MWPhoto () {
@@ -15,7 +16,8 @@ @interface MWPhoto () {
// Image Sources
NSString *_photoPath;
NSURL *_photoURL;
-
+ NSURL *_assetURL;
+
// Image
UIImage *_underlyingImage;
@@ -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 {
@@ -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];
@@ -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];
@@ -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.");
View
7 README.markdown
@@ -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`.
@@ -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
@@ -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.
Something went wrong with that request. Please try again.