Skip to content

Commit

Permalink
MWPhoto now accepts PHAssets
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaterfall committed Jul 6, 2015
1 parent 46b4dd1 commit c72c22d
Show file tree
Hide file tree
Showing 23 changed files with 2,160 additions and 2,023 deletions.
3 changes: 2 additions & 1 deletion Example/MWPhotoBrowser/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

@property (nonatomic, strong) NSMutableArray *photos;
@property (nonatomic, strong) NSMutableArray *thumbs;
@property (nonatomic, strong) ALAssetsLibrary *assetLibrary;
@property (nonatomic, strong) NSMutableArray *assets;

@property (nonatomic, strong) ALAssetsLibrary *ALAssetsLibrary;

- (void)loadAssets;

@end
157 changes: 109 additions & 48 deletions Example/MWPhotoBrowser/Menu.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright 2010 d3i. All rights reserved.
//

#import <Photos/Photos.h>
#import "Menu.h"
#import "SDImageCache.h"
#import "MWCommon.h"
Expand Down Expand Up @@ -1016,9 +1017,24 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
case 7: {
@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]]];
if (NSClassFromString(@"PHAsset")) {
// Photos library
UIScreen *screen = [UIScreen mainScreen];
CGFloat scale = screen.scale;
// Sizing is very rough... more thought required in a real implementation
CGFloat imageSize = MAX(screen.bounds.size.width, screen.bounds.size.height);
CGSize imageTargetSize = CGSizeMake(imageSize * scale, imageSize * scale);
CGSize thumbTargetSize = CGSizeMake(imageSize / 3.0 * scale, imageSize / 3.0 * scale);
for (PHAsset *asset in copy) {
[photos addObject:[MWPhoto photoWithAsset:asset targetSize:imageTargetSize]];
[thumbs addObject:[MWPhoto photoWithAsset:asset targetSize:thumbTargetSize]];
}
} else {
// Assets library
for (ALAsset *asset in copy) {
[photos addObject:[MWPhoto photoWithURL:asset.defaultRepresentation.url]];
[thumbs addObject:[MWPhoto photoWithImage:[UIImage imageWithCGImage:asset.thumbnail]]];
}
}
}
break;
Expand All @@ -1037,7 +1053,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
browser.zoomPhotosToFill = YES;
browser.enableGrid = enableGrid;
browser.startOnGrid = startOnGrid;
browser.enableSwipeToDismiss = YES;
browser.enableSwipeToDismiss = NO;
[browser setCurrentPhotoIndex:0];

// Reset selections
Expand Down Expand Up @@ -1146,59 +1162,104 @@ - (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser {
#pragma mark - Load Assets

- (void)loadAssets {
if (NSClassFromString(@"PHAsset")) {

// Check library permissions
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusNotDetermined) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
[self performLoadAssets];
}
}];
} else if (status == PHAuthorizationStatusAuthorized) {
[self performLoadAssets];
}

} else {

// Assets library
[self performLoadAssets];

}
}

- (void)performLoadAssets {

// 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];
// Load
if (NSClassFromString(@"PHAsset")) {

// 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 performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
}
}
failureBlock:^(NSError *error){
NSLog(@"operation was not successfull!");
}];

}
// Photos library iOS >= 8
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
PHFetchOptions *options = [PHFetchOptions new];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *fetchResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:options];
[fetchResults enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[_assets addObject:obj];
}];
if (fetchResults.count > 0) {
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
};
});

// Process groups
void (^ assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if (group != nil) {
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];
[assetGroups addObject:group];
}
};
} else {

// Process!
[self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {
NSLog(@"There is an error");
}];
// Assets Library iOS < 8
_ALAssetsLibrary = [[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;
[_ALAssetsLibrary assetForURL:url
resultBlock:^(ALAsset *asset) {
if (asset) {
@synchronized(_assets) {
[_assets addObject:asset];
if (_assets.count == 1) {
// Added first asset so reload data
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
}
}
failureBlock:^(NSError *error){
NSLog(@"operation was not successfull!");
}];

}
}
};

// Process groups
void (^ assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if (group != nil) {
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];
[assetGroups addObject:group];
}
};

// Process!
[_ALAssetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:^(NSError *error) {
NSLog(@"There is an error");
}];

});

}

}

Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PODS:
- FBSnapshotTestCase (~> 1.8)
- FBSnapshotTestCase (1.8.1)
- MBProgressHUD (0.9.1)
- MWPhotoBrowser (2.0.0):
- MWPhotoBrowser (2.0.1):
- DACircularProgress (~> 2.3)
- MBProgressHUD (~> 0.9)
- SDWebImage (!= 3.7.2, ~> 3.7)
Expand All @@ -32,7 +32,7 @@ SPEC CHECKSUMS:
Expecta+Snapshots: ca15bfb57e7a0f78f86c7699c2c54ffacfa4ad2a
FBSnapshotTestCase: 3dc3899168747a0319c5278f5b3445c13a6532dd
MBProgressHUD: c47f2c166c126cf2ce36498d80f33e754d4e93ad
MWPhotoBrowser: fde3119a2698a6a9a90d66cde2fb4e80db3ab5fb
MWPhotoBrowser: 48930655b7f04d4394abd12a212e8892b8f79a10
SDWebImage: ed3095af2ff88b436426037444979b917f6c5575
Specta: 9cec98310dca411f7c7ffd6943552b501622abfe

Expand Down
17 changes: 9 additions & 8 deletions Example/Pods/Local Podspecs/MWPhotoBrowser.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c72c22d

Please sign in to comment.