Skip to content

Commit

Permalink
Merge branch 'release/0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaoj committed May 22, 2014
2 parents 756ee5b + 86a09ba commit dff2512
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 71 deletions.
4 changes: 2 additions & 2 deletions BSImagePicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "BSImagePicker"
s.version = "0.2"
s.version = "0.3"
s.summary = "BSImagePicker is a multiple image picker for iOS"
s.description = <<-DESC
A mix between the native iOS 7 gallery and facebooks image picker. Allows you to preview and select multiple images.
Expand All @@ -9,7 +9,7 @@ Pod::Spec.new do |s|
s.license = "MIT"
s.author = { "Joakim Gyllström" => "[email protected]" }
s.platform = :ios, "7.0"
s.source = { :git => "https://github.com/mikaoj/BSImagePicker.git", :tag => "0.2" }
s.source = { :git => "https://github.com/mikaoj/BSImagePicker.git", :tag => "0.3" }
s.source_files = "BSImagePicker/{Category,View,Model,Controller}/*.{h,m}"
s.exclude_files = "BSImagePicker/Controller/BSAppDelegate.{h,m}"
s.public_header_files = "BSImagePicker/Controller/BSImagePickerController.h", "BSImagePicker/Category/UIViewController+MultipleImagePicker.h"
Expand Down
4 changes: 4 additions & 0 deletions BSImagePicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
5509A3FE192E90F4006E4DA1 /* BSPreviewCell.h in Headers */ = {isa = PBXBuildFile; fileRef = 556522E51926D42900492DCD /* BSPreviewCell.h */; };
5509A3FF192E90F9006E4DA1 /* BSPreviewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 556522E61926D42900492DCD /* BSPreviewCell.m */; };
556522E71926D42900492DCD /* BSPreviewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 556522E61926D42900492DCD /* BSPreviewCell.m */; };
556E0CE919017470006F8F73 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 556E0CE719017470006F8F73 /* InfoPlist.strings */; };
556E0CF0190174E6006F8F73 /* UIViewController+MultipleImagePicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 55A11EE018FC952D00F28CA5 /* UIViewController+MultipleImagePicker.m */; };
Expand Down Expand Up @@ -347,6 +349,7 @@
556E0D0B1901781B006F8F73 /* BSPhotoCell.h in Headers */,
556E0D0C1901781B006F8F73 /* BSCheckmarkView.h in Headers */,
556E0D0D1901781B006F8F73 /* BSZoomOutAnimator.h in Headers */,
5509A3FE192E90F4006E4DA1 /* BSPreviewCell.h in Headers */,
556E0D0E1901781B006F8F73 /* BSZoomInAnimator.h in Headers */,
556E0D0F1901781B006F8F73 /* BSImageSelectionController.h in Headers */,
556E0D101901781B006F8F73 /* BSImagePreviewController.h in Headers */,
Expand Down Expand Up @@ -495,6 +498,7 @@
files = (
556E0CF0190174E6006F8F73 /* UIViewController+MultipleImagePicker.m in Sources */,
556E0CF2190174E6006F8F73 /* ALAsset+Equal.m in Sources */,
5509A3FF192E90F9006E4DA1 /* BSPreviewCell.m in Sources */,
556E0CF3190174E6006F8F73 /* BSSpeechBubbleView.m in Sources */,
556E0CF4190174E6006F8F73 /* BSTriangleView.m in Sources */,
556E0CF5190174E6006F8F73 /* BSAlbumCell.m in Sources */,
Expand Down
7 changes: 0 additions & 7 deletions BSImagePicker/Controller/BSImagePickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
return self;
}

- (void)didReceiveMemoryWarning
{
[self setImagePicker:nil];

[super didReceiveMemoryWarning];
}

#pragma mark - Lazy load

- (BSImageSelectionController *)imagePicker
Expand Down
15 changes: 13 additions & 2 deletions BSImagePicker/Controller/BSImagePreviewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ - (void)viewWillAppear:(BOOL)animated
[self setupRightButton];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[self.collectionView reloadData];

//Scroll to the correct image
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentAssetIndex inSection:0]
atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:NO];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
Expand All @@ -90,7 +101,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
options:0
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result) {
[cell.imageView setImage:[UIImage imageWithCGImage:result.defaultRepresentation.fullScreenImage]];
[cell.imageView setImage:[UIImage imageWithCGImage:result.defaultRepresentation.fullResolutionImage]];
}
}];

Expand All @@ -101,7 +112,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return self.view.bounds.size;
return self.collectionView.bounds.size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
Expand Down
165 changes: 132 additions & 33 deletions BSImagePicker/Controller/BSImageSelectionController.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ + (ALAssetsLibrary *)defaultAssetsLibrary;
@property (nonatomic, strong) BSZoomInAnimator *zoomInAnimator;
@property (nonatomic, strong) BSZoomOutAnimator *zoomOutAnimator;

- (void)setupAlbums;
- (void)setupItemSizeForAssetsGroup:(ALAssetsGroup *)group;

- (void)finishButtonPressed:(id)sender;
- (void)albumButtonPressed:(id)sender;

- (void)cellLongPressed:(UIGestureRecognizer *)recognizer;

- (void)registerCollectionViewCellIdentifiers;
- (void)registerTableViewCellIdentifiers;

- (void)showAlbumView;
- (void)hideAlbumView;

- (void)reloadPhotosAndScrollToTop;

- (void)recievedAssetsNotification:(NSNotification *)notification;

@end

@implementation BSImageSelectionController
Expand Down Expand Up @@ -92,35 +100,32 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
[self setPhotoAlbums:[[NSMutableArray alloc] init]];
[self setSelectedPhotos:[[NSMutableArray alloc] init]];

//Find all albums
[[BSImageSelectionController defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if(group) {
//Default to select saved photos album
if([[group valueForProperty:ALAssetsGroupPropertyType] isEqual:[NSNumber numberWithInteger:ALAssetsGroupSavedPhotos]]) {
[self.photoAlbums insertObject:group atIndex:0];
[self setSelectedAlbum:group];

//Set default item size if no size already given.
if(CGSizeEqualToSize(self.navigationController.itemSize, CGSizeZero)) {
//Get thumbnail size
CGSize thumbnailSize = CGSizeMake(CGImageGetWidth(group.posterImage), CGImageGetHeight(group.posterImage));

//We want 3 images in each row. So width should be viewWidth-(4*LEFT/RIGHT_INSET)/3
//4*2.0 is edgeinset
//Height should be adapted so we maintain the aspect ratio of thumbnail
//original height / original width * new width
CGSize itemSize = CGSizeMake((320.0 - (4*2.0))/3.0, 100);
[self.navigationController setItemSize:CGSizeMake(itemSize.width, thumbnailSize.height / thumbnailSize.width * itemSize.width)];
}
} else {
[self.photoAlbums addObject:group];
}
}
} failureBlock:nil];
[self setupAlbums];
}
return self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

//Release these if they aren't visible
if(![self.speechBubbleView isDescendantOfView:self.navigationController.view]) {
[self setSpeechBubbleView:nil];
[self setAlbumTableView:nil];
[self setCoverView:nil];
}

//Release preview controller if we aren't previewing
if(![self.navigationController.viewControllers containsObject:self.imagePreviewController]) {
[self setImagePreviewController:nil];
}

//These can be released at any time
[self setZoomInAnimator:nil];
[self setZoomOutAnimator:nil];
}

#pragma mark - UIViewController

- (void)viewWillAppear:(BOOL)animated
Expand All @@ -141,6 +146,15 @@ - (void)viewWillAppear:(BOOL)animated
} else {
[self.doneButton setEnabled:NO];
}

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recievedAssetsNotification:) name:ALAssetsLibraryChangedNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

[[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - UICollectionViewDataSource
Expand Down Expand Up @@ -172,7 +186,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
}

[self.selectedAlbum enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:indexPath.row]
options:0
options:NSEnumerationReverse
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result) {
[cell setAssetIndex:index];
Expand All @@ -195,7 +209,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtInde
BOOL allow = NO;
if([self.selectedPhotos count] < self.navigationController.maximumNumberOfImages) {
[self.selectedAlbum enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:indexPath.row]
options:0
options:NSEnumerationReverse
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result) {
//Enable done button
Expand All @@ -220,7 +234,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtInde
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self.selectedAlbum enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:indexPath.row]
options:0
options:NSEnumerationReverse
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result) {
if(self.navigationController.toggleBlock) {
Expand Down Expand Up @@ -318,6 +332,8 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

if(![group isEqual:self.selectedAlbum]) {
[self setSelectedAlbum:group];

[self reloadPhotosAndScrollToTop];
}

[self hideAlbumView];
Expand Down Expand Up @@ -424,6 +440,8 @@ - (UITableView *)albumTableView
[_albumTableView setDelegate:self];
[_albumTableView setDataSource:self];
[self registerTableViewCellIdentifiers];

[_albumTableView reloadData];
}

return _albumTableView;
Expand All @@ -432,7 +450,7 @@ - (UITableView *)albumTableView
- (UIView *)coverView
{
if(!_coverView) {
_coverView = [[UIView alloc] initWithFrame:self.navigationController.view.frame];
_coverView = [[UIView alloc] initWithFrame:self.navigationController.view.bounds];
[_coverView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAlbumView)];
Expand Down Expand Up @@ -532,8 +550,6 @@ - (void)setSelectedAlbum:(ALAssetsGroup *)selectedAlbum
{
_selectedAlbum = selectedAlbum;
[self.albumButton setTitle:[_selectedAlbum valueForProperty:ALAssetsGroupPropertyName] forState:UIControlStateNormal];

[self.collectionView reloadData];
}

- (void)registerCollectionViewCellIdentifiers
Expand All @@ -549,9 +565,7 @@ - (void)registerTableViewCellIdentifiers
- (void)showAlbumView
{
[self.navigationController.view addSubview:self.coverView];

[self.navigationController.view addSubview:self.speechBubbleView];
[self.albumTableView reloadData];

CGFloat tableViewHeight = MIN(self.albumTableView.contentSize.height, 240);
CGRect frame = CGRectMake(0, 0, self.speechBubbleView.frame.size.width, tableViewHeight+7);
Expand Down Expand Up @@ -600,4 +614,89 @@ - (void)hideAlbumView
}];
}

- (void)setupAlbums
{
//Clear previous albums
[self.photoAlbums removeAllObjects];

//Find all albums
[[BSImageSelectionController defaultAssetsLibrary] enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if(group) {
//Default to select saved photos album
if([[group valueForProperty:ALAssetsGroupPropertyType] isEqual:[NSNumber numberWithInteger:ALAssetsGroupSavedPhotos]]) {
[self.photoAlbums insertObject:group atIndex:0];

//Set it to be the selected album if we have no album selected
if(!self.selectedAlbum) {
[self setupItemSizeForAssetsGroup:group];
[self setSelectedAlbum:group];

[self.albumTableView reloadData];
[self reloadPhotosAndScrollToTop];
}
} else {
[self.photoAlbums addObject:group];
}
} else {
[self.albumTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
} failureBlock:nil];
}

- (void)setupItemSizeForAssetsGroup:(ALAssetsGroup *)group
{
//Set default item size if no size already given.
if(CGSizeEqualToSize(self.navigationController.itemSize, CGSizeZero)) {
//Get thumbnail size
CGSize thumbnailSize = CGSizeMake(CGImageGetWidth(group.posterImage), CGImageGetHeight(group.posterImage));

//We want 3 images in each row. So width should be viewWidth-(4*LEFT/RIGHT_INSET)/3
//4*2.0 is edgeinset
//Height should be adapted so we maintain the aspect ratio of thumbnail
//original height / original width * new width
CGSize itemSize = CGSizeMake((320.0 - (4*2.0))/3.0, 100);
[self.navigationController setItemSize:CGSizeMake(itemSize.width, thumbnailSize.height / thumbnailSize.width * itemSize.width)];
}
}

- (void)recievedAssetsNotification:(NSNotification *)notification
{
NSSet *updatedAssets = [notification.userInfo objectForKey:ALAssetLibraryUpdatedAssetsKey];
NSSet *insertedAssetGroups = [notification.userInfo objectForKey:ALAssetLibraryInsertedAssetGroupsKey];
NSSet *updatedAssetGroups = [notification.userInfo objectForKey:ALAssetLibraryUpdatedAssetGroupsKey];
NSSet *deletedAssetGroups = [notification.userInfo objectForKey:ALAssetLibraryDeletedAssetGroupsKey];

if([updatedAssets isKindOfClass:[NSSet class]] && [updatedAssets count] > 0) {
//This is what we should do:
//Loop through all assets of selected album to see if they match.
//If so find out which index path they have
//Add indexpath to array
//Update indexpaths in collectionview
//But fuck that, lets keep it simple and see how that plays out
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
});
}

if( ([insertedAssetGroups isKindOfClass:[NSSet class]] && [insertedAssetGroups count] > 0)
|| ([updatedAssetGroups isKindOfClass:[NSSet class]] && [updatedAssetGroups count] > 0)
|| ([deletedAssetGroups isKindOfClass:[NSSet class]] && [deletedAssetGroups count] > 0)) {
dispatch_async(dispatch_get_main_queue(), ^{
[self setupAlbums];
});
}
}

- (void)reloadPhotosAndScrollToTop
{
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
if(self.selectedAlbum.numberOfAssets > 0) {
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
atScrollPosition:UICollectionViewScrollPositionTop
animated:YES];
}
} completion:nil];
}

@end
2 changes: 2 additions & 0 deletions BSImagePicker/View/BSPreviewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
[self addSubview:self.scrollView];
}
return self;
Expand All @@ -53,6 +54,7 @@ - (UIScrollView *)scrollView
{
if(!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
[_scrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
[_scrollView setMinimumZoomScale:1.0];
[_scrollView setMaximumZoomScale:3.0];
[_scrollView setDelegate:self];
Expand Down
2 changes: 1 addition & 1 deletion BSImagePicker/View/BSSpeechBubbleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ - (id)initWithFrame:(CGRect)frame
[self addSubview:self.triangle];

[self.layer setCornerRadius:4.0];
[self setClipsToBounds:YES];
[self setClipsToBounds:NO];
}
return self;
}
Expand Down
Loading

0 comments on commit dff2512

Please sign in to comment.