diff --git a/QBImagePicker/QBAssetsViewController.m b/QBImagePicker/QBAssetsViewController.m index 98177684..4062fcfa 100644 --- a/QBImagePicker/QBAssetsViewController.m +++ b/QBImagePicker/QBAssetsViewController.m @@ -57,6 +57,8 @@ - (NSArray *)qb_indexPathsForElementsInRect:(CGRect)rect @interface QBAssetsViewController () @property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton; +@property (weak, nonatomic) IBOutlet UIView *activityContainerView; +@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator; @property (nonatomic, strong) PHFetchResult *fetchResult; @@ -73,14 +75,38 @@ @implementation QBAssetsViewController - (void)viewDidLoad { [super viewDidLoad]; - + [self setUpToolbarItems]; [self resetCachedAssets]; + if (self.imagePickerController.downloadiCloudPhotos) { + [self setupActivityContainerView]; + } // Register observer [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self]; } +- (void)setupActivityContainerView { + [self.view addSubview:self.activityContainerView]; + [self addConstriantsToActivityView:self.activityContainerView subView:self.view]; +} + +- (void)addConstriantsToActivityView:(UIView *)subView subView: (UIView *)superView { + subView.translatesAutoresizingMaskIntoConstraints = false; + + NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]; + + NSLayoutConstraint *trailingConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]; + + NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; + + NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]; + + NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[[UIScreen mainScreen] bounds].size.height]; + + [superView addConstraints:@[leadingConstraint, trailingConstraint, topConstraint, bottomConstraint, heightConstraint]]; +} + - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; @@ -107,10 +133,10 @@ - (void)viewWillAppear:(BOOL)animated if (self.fetchResult.count > 0 && self.isMovingToParentViewController && !self.disableScrollToBottom) { // when presenting as a .FormSheet on iPad, the frame is not correct until just after viewWillAppear: // dispatching to the main thread waits one run loop until the frame is update and the layout is complete - dispatch_async(dispatch_get_main_queue(), ^{ + [self.collectionView performBatchUpdates:^{} completion: ^(BOOL finished) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(self.fetchResult.count - 1) inSection:0]; [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO]; - }); + }]; } } @@ -603,7 +629,21 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa } } else { if ([imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerController:didFinishPickingAssets:)]) { - [imagePickerController.delegate qb_imagePickerController:imagePickerController didFinishPickingAssets:@[asset]]; + + if ([asset isKindOfClass:[PHAsset class]] && self.imagePickerController.downloadiCloudPhotos) { + [self showActivityIndicator:true]; + PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; + [options setNetworkAccessAllowed:true]; + [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) { + UIImage *image = [UIImage imageWithData:imageData scale:1.0]; + if (image) { + [self showActivityIndicator:false]; + [imagePickerController.delegate qb_imagePickerController:imagePickerController didFinishPickingAssets:@[asset]]; + } + }]; + } else { + [imagePickerController.delegate qb_imagePickerController:imagePickerController didFinishPickingAssets:@[asset]]; + } } } @@ -612,6 +652,12 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa } } +- (void)showActivityIndicator:(BOOL) show { + [self.view bringSubviewToFront:self.activityContainerView]; + self.activityContainerView.hidden = !show; + (show ? [self.activityIndicator startAnimating] : [self.activityIndicator stopAnimating]); +} + - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { if (!self.imagePickerController.allowsMultipleSelection) { diff --git a/QBImagePicker/QBImagePicker.storyboard b/QBImagePicker/QBImagePicker.storyboard index cba77d90..44c898d0 100644 --- a/QBImagePicker/QBImagePicker.storyboard +++ b/QBImagePicker/QBImagePicker.storyboard @@ -1,5 +1,8 @@ - - + + + + + @@ -262,10 +265,23 @@ + + + diff --git a/QBImagePicker/QBImagePickerController.h b/QBImagePicker/QBImagePickerController.h index e7547e59..ddca4918 100644 --- a/QBImagePicker/QBImagePickerController.h +++ b/QBImagePicker/QBImagePickerController.h @@ -48,4 +48,6 @@ typedef NS_ENUM(NSUInteger, QBImagePickerMediaType) { @property (nonatomic, assign) NSUInteger numberOfColumnsInPortrait; @property (nonatomic, assign) NSUInteger numberOfColumnsInLandscape; +@property (nonatomic, assign) BOOL downloadiCloudPhotos; + @end