Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Add support in the Picker to download iCloud image before returning #212

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions QBImagePicker/QBAssetsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ - (NSArray *)qb_indexPathsForElementsInRect:(CGRect)rect
@interface QBAssetsViewController () <PHPhotoLibraryChangeObserver, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) IBOutlet UIBarButtonItem *doneButton;
@property (weak, nonatomic) IBOutlet UIView *activityContainerView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@property (nonatomic, strong) PHFetchResult *fetchResult;

Expand All @@ -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];
Expand All @@ -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];
});
}];
}
}

Expand Down Expand Up @@ -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]];
}
}
}

Expand All @@ -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) {
Expand Down
20 changes: 18 additions & 2 deletions QBImagePicker/QBImagePicker.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16A323" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
Expand Down Expand Up @@ -262,10 +265,23 @@
</barButtonItem>
</navigationItem>
<connections>
<outlet property="activityContainerView" destination="LYR-It-Jht" id="J1p-mI-O1R"/>
<outlet property="activityIndicator" destination="JJu-1L-0Pg" id="sSS-SO-kib"/>
<outlet property="doneButton" destination="nai-ZV-lR8" id="lxY-18-MpF"/>
</connections>
</collectionViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="qBb-2Q-SxP" userLabel="First Responder" sceneMemberID="firstResponder"/>
<view hidden="YES" contentMode="scaleToFill" id="LYR-It-Jht" userLabel="ActivityIndicatorContainerView">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" hidesWhenStopped="YES" animating="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="JJu-1L-0Pg">
<rect key="frame" x="177" y="323" width="20" height="20"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
</activityIndicatorView>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.65071875000000001" colorSpace="calibratedWhite"/>
</view>
</objects>
<point key="canvasLocation" x="1814" y="852"/>
</scene>
Expand Down
2 changes: 2 additions & 0 deletions QBImagePicker/QBImagePickerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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