- Fix not show picker when close native image picker controller

This commit is contained in:
Giuseppe Nucifora 2016-04-21 13:41:40 +02:00
parent 60b14b37a7
commit efc65445a7
2 changed files with 89 additions and 89 deletions

View File

@ -49,9 +49,7 @@
[super updateConstraints]; [super updateConstraints];
if (!self.didUpdateConstraints) { if (!self.didUpdateConstraints) {
self.didUpdateConstraints = YES;
[self.contentView autoPinEdgesToSuperviewEdges]; [self.contentView autoPinEdgesToSuperviewEdges];
[_photoImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:8]; [_photoImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:8];
@ -64,6 +62,8 @@
[_loadingSpinner autoAlignAxisToSuperviewAxis:ALAxisHorizontal]; [_loadingSpinner autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[_loadingSpinner autoAlignAxisToSuperviewAxis:ALAxisVertical]; [_loadingSpinner autoAlignAxisToSuperviewAxis:ALAxisVertical];
self.didUpdateConstraints = YES;
} }
} }

View File

@ -55,33 +55,33 @@
_targetSize = CGSizeMake(1024, 1024); _targetSize = CGSizeMake(1024, 1024);
_haveCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; _haveCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
_animationTime = 0.4; _animationTime = 0.4;
} }
return self; return self;
} }
- (void) viewDidLoad { - (void) viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor]; self.view.backgroundColor = [UIColor clearColor];
_imagePickerView = [UIView newAutoLayoutView]; _imagePickerView = [UIView newAutoLayoutView];
[_imagePickerView setBackgroundColor:[UIColor whiteColor]]; [_imagePickerView setBackgroundColor:[UIColor whiteColor]];
_backgroundView = [UIView newAutoLayoutView]; _backgroundView = [UIView newAutoLayoutView];
_backgroundView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.7]; _backgroundView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.7];
_backgroundView.alpha = 0; _backgroundView.alpha = 0;
UITapGestureRecognizer *dismissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)]; UITapGestureRecognizer *dismissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
_backgroundView.userInteractionEnabled = YES; _backgroundView.userInteractionEnabled = YES;
[_backgroundView addGestureRecognizer:dismissTap]; [_backgroundView addGestureRecognizer:dismissTap];
[self.view addSubview:_backgroundView]; [self.view addSubview:_backgroundView];
UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init]; UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; [aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:aFlowLayout]; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:aFlowLayout];
[_collectionView setTranslatesAutoresizingMaskIntoConstraints:NO]; [_collectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_collectionView setBackgroundColor:[UIColor whiteColor]]; [_collectionView setBackgroundColor:[UIColor whiteColor]];
@ -90,33 +90,33 @@
_collectionView.delegate = self; _collectionView.delegate = self;
_collectionView.dataSource = self; _collectionView.dataSource = self;
[_collectionView registerClass:[PNCollectionViewCell class] forCellWithReuseIdentifier:[PNCollectionViewCell cellIdentifier]]; [_collectionView registerClass:[PNCollectionViewCell class] forCellWithReuseIdentifier:[PNCollectionViewCell cellIdentifier]];
UIFont *btnFont = [UIFont systemFontOfSize:19.0]; UIFont *btnFont = [UIFont systemFontOfSize:19.0];
_photoLibraryBtn = [UIButton newAutoLayoutView]; _photoLibraryBtn = [UIButton newAutoLayoutView];
[_photoLibraryBtn setTitle:NSLocalizedString(@"Photo Library",@"") forState:UIControlStateNormal]; [_photoLibraryBtn setTitle:NSLocalizedString(@"Photo Library",@"") forState:UIControlStateNormal];
_photoLibraryBtn.titleLabel.font = btnFont; _photoLibraryBtn.titleLabel.font = btnFont;
[_photoLibraryBtn addTarget:self action:@selector(selectFromLibraryWasPressed) forControlEvents:UIControlEventTouchUpInside]; [_photoLibraryBtn addTarget:self action:@selector(selectFromLibraryWasPressed) forControlEvents:UIControlEventTouchUpInside];
[_photoLibraryBtn setTitleColor:[@"0b60fe" colorFromHex] forState:UIControlStateNormal]; [_photoLibraryBtn setTitleColor:[@"0b60fe" colorFromHex] forState:UIControlStateNormal];
[_photoLibraryBtn setTitleColor:[@"70b3fd" colorFromHex] forState:UIControlStateHighlighted]; [_photoLibraryBtn setTitleColor:[@"70b3fd" colorFromHex] forState:UIControlStateHighlighted];
_cancelBtn = [UIButton newAutoLayoutView]; _cancelBtn = [UIButton newAutoLayoutView];
[_cancelBtn setTitle:NSLocalizedString(@"Cancel",@"") forState:UIControlStateNormal]; [_cancelBtn setTitle:NSLocalizedString(@"Cancel",@"") forState:UIControlStateNormal];
_cancelBtn.titleLabel.font = btnFont; _cancelBtn.titleLabel.font = btnFont;
[_cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; [_cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[_cancelBtn setTitleColor:[@"0b60fe" colorFromHex] forState:UIControlStateNormal]; [_cancelBtn setTitleColor:[@"0b60fe" colorFromHex] forState:UIControlStateNormal];
[_cancelBtn setTitleColor:[@"70b3fd" colorFromHex] forState:UIControlStateHighlighted]; [_cancelBtn setTitleColor:[@"70b3fd" colorFromHex] forState:UIControlStateHighlighted];
_separator2 = [UIView newAutoLayoutView]; _separator2 = [UIView newAutoLayoutView];
_separator2.backgroundColor = [@"cacaca" colorFromHex]; _separator2.backgroundColor = [@"cacaca" colorFromHex];
[_imagePickerView addSubview:_separator2]; [_imagePickerView addSubview:_separator2];
_separator3 = [UIView newAutoLayoutView]; _separator3 = [UIView newAutoLayoutView];
_separator3.backgroundColor = [@"cacaca" colorFromHex]; _separator3.backgroundColor = [@"cacaca" colorFromHex];
[_imagePickerView addSubview:_separator3]; [_imagePickerView addSubview:_separator3];
if(_haveCamera) { if(_haveCamera) {
_cameraBtn = [UIButton newAutoLayoutView]; _cameraBtn = [UIButton newAutoLayoutView];
[_cameraBtn setTitle:NSLocalizedString(@"Take Photo",@"") forState:UIControlStateNormal]; [_cameraBtn setTitle:NSLocalizedString(@"Take Photo",@"") forState:UIControlStateNormal];
@ -126,38 +126,38 @@
[_cameraBtn setTitleColor:[@"70b3fd" colorFromHex] forState:UIControlStateHighlighted]; [_cameraBtn setTitleColor:[@"70b3fd" colorFromHex] forState:UIControlStateHighlighted];
_cameraBtn.hidden = !_haveCamera; _cameraBtn.hidden = !_haveCamera;
[_imagePickerView addSubview:_cameraBtn]; [_imagePickerView addSubview:_cameraBtn];
_separator1 = [UIView newAutoLayoutView]; _separator1 = [UIView newAutoLayoutView];
_separator1.backgroundColor = [@"cacaca" colorFromHex]; _separator1.backgroundColor = [@"cacaca" colorFromHex];
[_imagePickerView addSubview:_separator1]; [_imagePickerView addSubview:_separator1];
} }
[_imagePickerView addSubview:_collectionView]; [_imagePickerView addSubview:_collectionView];
[_imagePickerView addSubview:_photoLibraryBtn]; [_imagePickerView addSubview:_photoLibraryBtn];
[_imagePickerView addSubview:_cancelBtn]; [_imagePickerView addSubview:_cancelBtn];
[self.view addSubview:_imagePickerView]; [self.view addSubview:_imagePickerView];
[self.view setNeedsUpdateConstraints]; [self.view setNeedsUpdateConstraints];
[_imagePickerView setNeedsUpdateConstraints]; [_imagePickerView setNeedsUpdateConstraints];
[_collectionView setNeedsUpdateConstraints]; [_collectionView setNeedsUpdateConstraints];
[_backgroundView setNeedsUpdateConstraints]; [_backgroundView setNeedsUpdateConstraints];
} }
- (void) updateViewConstraints { - (void) updateViewConstraints {
if (!_didUpdateConstraints) { if (!_didUpdateConstraints) {
_didUpdateConstraints = YES; _didUpdateConstraints = YES;
[_backgroundView autoPinEdgesToSuperviewEdges]; [_backgroundView autoPinEdgesToSuperviewEdges];
[_imagePickerView autoPinEdgeToSuperviewEdge:ALEdgeLeading]; [_imagePickerView autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[_imagePickerView autoPinEdgeToSuperviewEdge:ALEdgeTrailing]; [_imagePickerView autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
_hideConstraint = [_imagePickerView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:-imagePickerHeight]; _hideConstraint = [_imagePickerView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:-imagePickerHeight];
[_imagePickerView autoSetDimension:ALDimensionHeight toSize:100 relation:NSLayoutRelationGreaterThanOrEqual]; [_imagePickerView autoSetDimension:ALDimensionHeight toSize:100 relation:NSLayoutRelationGreaterThanOrEqual];
[_cancelBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading]; [_cancelBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[_cancelBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing]; [_cancelBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
[_cancelBtn autoAlignAxisToSuperviewAxis:ALAxisVertical]; [_cancelBtn autoAlignAxisToSuperviewAxis:ALAxisVertical];
@ -165,13 +165,13 @@
[_cancelBtn autoSetDimension:ALDimensionHeight toSize:30]; [_cancelBtn autoSetDimension:ALDimensionHeight toSize:30];
[_cancelBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10]; [_cancelBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10];
[_cancelBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10]; [_cancelBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10];
[_separator3 autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10]; [_separator3 autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10];
[_separator3 autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10]; [_separator3 autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10];
[_separator3 autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_cancelBtn withOffset:-10]; [_separator3 autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_cancelBtn withOffset:-10];
[_separator3 autoAlignAxisToSuperviewAxis:ALAxisVertical]; [_separator3 autoAlignAxisToSuperviewAxis:ALAxisVertical];
[_separator3 autoSetDimension:ALDimensionHeight toSize:1]; [_separator3 autoSetDimension:ALDimensionHeight toSize:1];
[_photoLibraryBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading]; [_photoLibraryBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[_photoLibraryBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing]; [_photoLibraryBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
[_photoLibraryBtn autoAlignAxisToSuperviewAxis:ALAxisVertical]; [_photoLibraryBtn autoAlignAxisToSuperviewAxis:ALAxisVertical];
@ -179,13 +179,13 @@
[_photoLibraryBtn autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_separator3 withOffset:-10]; [_photoLibraryBtn autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_separator3 withOffset:-10];
[_photoLibraryBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10]; [_photoLibraryBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10];
[_photoLibraryBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10]; [_photoLibraryBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10];
[_separator2 autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10]; [_separator2 autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10];
[_separator2 autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10]; [_separator2 autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10];
[_separator2 autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_photoLibraryBtn withOffset:-10]; [_separator2 autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_photoLibraryBtn withOffset:-10];
[_separator2 autoAlignAxisToSuperviewAxis:ALAxisVertical]; [_separator2 autoAlignAxisToSuperviewAxis:ALAxisVertical];
[_separator2 autoSetDimension:ALDimensionHeight toSize:1]; [_separator2 autoSetDimension:ALDimensionHeight toSize:1];
if (_haveCamera) { if (_haveCamera) {
[_cameraBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading]; [_cameraBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[_cameraBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing]; [_cameraBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
@ -194,14 +194,14 @@
[_cameraBtn autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_separator2 withOffset:-10]; [_cameraBtn autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_separator2 withOffset:-10];
[_cameraBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10]; [_cameraBtn autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10];
[_cameraBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10]; [_cameraBtn autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10];
[_separator1 autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10]; [_separator1 autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10];
[_separator1 autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10]; [_separator1 autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:10];
[_separator1 autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_cameraBtn withOffset:-10]; [_separator1 autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_cameraBtn withOffset:-10];
[_separator1 autoAlignAxisToSuperviewAxis:ALAxisVertical]; [_separator1 autoAlignAxisToSuperviewAxis:ALAxisVertical];
[_separator1 autoSetDimension:ALDimensionHeight toSize:1]; [_separator1 autoSetDimension:ALDimensionHeight toSize:1];
} }
[_collectionView autoPinEdgeToSuperviewEdge:ALEdgeLeading]; [_collectionView autoPinEdgeToSuperviewEdge:ALEdgeLeading];
[_collectionView autoPinEdgeToSuperviewEdge:ALEdgeTrailing]; [_collectionView autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
[_collectionView autoAlignAxisToSuperviewAxis:ALAxisVertical]; [_collectionView autoAlignAxisToSuperviewAxis:ALAxisVertical];
@ -212,16 +212,16 @@
else { else {
[_collectionView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_separator2 withOffset:-15]; [_collectionView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:_separator2 withOffset:-15];
} }
[_collectionView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10]; [_collectionView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10];
} }
[super updateViewConstraints]; [super updateViewConstraints];
} }
- (void) viewDidAppear:(BOOL)animated { - (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; [super viewDidAppear:animated];
if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) { if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
// Will get here on both iOS 7 & 8 even though camera permissions weren't required // Will get here on both iOS 7 & 8 even though camera permissions weren't required
@ -265,9 +265,9 @@
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[PNCollectionViewCell cellIdentifier] forIndexPath:indexPath]; PNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[PNCollectionViewCell cellIdentifier] forIndexPath:indexPath];
PHAsset *asset = _assets[_assets.count-1 - indexPath.row]; PHAsset *asset = _assets[_assets.count-1 - indexPath.row];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
options.networkAccessAllowed = YES; options.networkAccessAllowed = YES;
@ -282,7 +282,7 @@
} }
}); });
}; };
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(300, 300) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) { [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(300, 300) contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
// Check if the request was successful. // Check if the request was successful.
if (!result) { if (!result) {
@ -293,14 +293,14 @@
[cell setNeedsUpdateConstraints]; [cell setNeedsUpdateConstraints];
}); });
}]; }];
return cell; return cell;
} }
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES]; [collectionView deselectItemAtIndexPath:indexPath animated:YES];
PHAsset *asset = _assets[_assets.count-1 - indexPath.row]; PHAsset *asset = _assets[_assets.count-1 - indexPath.row];
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat; options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
@ -316,24 +316,24 @@
} }
}); });
}; };
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:_targetSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) { [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:_targetSize contentMode:PHImageContentModeAspectFit options:options resultHandler:^(UIImage *result, NSDictionary *info) {
// Hide the progress view now the request has completed. // Hide the progress view now the request has completed.
// Check if the request was successful. // Check if the request was successful.
if (!result) { if (!result) {
return; return;
} }
// Show the UIImageView and use it to display the requested image. // Show the UIImageView and use it to display the requested image.
if ([delegate respondsToSelector:@selector(imagePicker:didSelectImage:)]) { if ([delegate respondsToSelector:@selector(imagePicker:didSelectImage:)]) {
[delegate imagePicker:self didSelectImage:result]; [delegate imagePicker:self didSelectImage:result];
} }
[self dismissAnimated:YES]; [self dismissAnimated:YES];
}]; }];
} }
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
@ -348,20 +348,20 @@
- (void)getCameraRollImages { - (void)getCameraRollImages {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
PHFetchOptions *allPhotosOptions = [PHFetchOptions new]; PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]]; allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
//[allPhotosOptions setFetchLimit:20]; //[allPhotosOptions setFetchLimit:20];
PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions]; PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];
[allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) { [allPhotosResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
if(asset) { if(asset) {
[_assets addObject:asset]; [_assets addObject:asset];
} }
}]; }];
[_collectionView reloadData]; [_collectionView reloadData];
}); });
} }
@ -369,22 +369,24 @@
- (void)takePhotoWasPressed { - (void)takePhotoWasPressed {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error" UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera" message:@"Device has no camera"
delegate:nil delegate:nil
cancelButtonTitle:@"OK" cancelButtonTitle:@"OK"
otherButtonTitles: nil]; otherButtonTitles: nil];
[myAlertView show]; [myAlertView show];
} else { } else {
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self; picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil]; picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
[self presentViewController:picker animated:YES completion:nil]; [_targetController presentViewController:picker animated:YES completion:^{
[self dismissAnimated:YES];
}];
} }
} }
@ -393,13 +395,15 @@
picker.delegate = self; picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil]; picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
[self presentViewController:picker animated:YES completion:nil]; [_targetController presentViewController:picker animated:YES completion:^{
[self dismissAnimated:YES];
}];
} }
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage]; UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{ [picker dismissViewControllerAnimated:YES completion:^{
if ([delegate respondsToSelector:@selector(imagePicker:didSelectImage:)]) { if ([delegate respondsToSelector:@selector(imagePicker:didSelectImage:)]) {
[delegate imagePicker:self didSelectImage:chosenImage]; [delegate imagePicker:self didSelectImage:chosenImage];
@ -409,7 +413,9 @@
} }
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil]; [picker dismissViewControllerAnimated:YES completion:^{
[self showImagePickerInController:_targetController];
}];
} }
#pragma mark - Show #pragma mark - Show
@ -420,32 +426,34 @@
- (void)showImagePickerInController:(UIViewController *)controller animated:(BOOL)animated { - (void)showImagePickerInController:(UIViewController *)controller animated:(BOOL)animated {
if (_isVisible != YES) { if (_isVisible != YES) {
_targetController = controller;
if ([delegate respondsToSelector:@selector(imagePickerWillOpen)]) { if ([delegate respondsToSelector:@selector(imagePickerWillOpen)]) {
[delegate imagePickerWillOpen]; [delegate imagePickerWillOpen];
} }
_isVisible = YES; _isVisible = YES;
[self setTransitioningDelegate:transitionController]; [self setTransitioningDelegate:transitionController];
self.modalPresentationStyle = UIModalPresentationOverCurrentContext; self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[controller presentViewController:self animated:NO completion:^{ [_targetController presentViewController:self animated:NO completion:^{
[_imagePickerView setNeedsUpdateConstraints]; [_imagePickerView setNeedsUpdateConstraints];
[_imagePickerView updateConstraintsIfNeeded]; [_imagePickerView updateConstraintsIfNeeded];
if (animated) { if (animated) {
[UIView animateWithDuration:_animationTime [UIView animateWithDuration:_animationTime
delay:0.0 delay:0.0
usingSpringWithDamping:1 usingSpringWithDamping:1
initialSpringVelocity:0 initialSpringVelocity:0
options:0 options:0
animations:^{ animations:^{
[_hideConstraint setConstant:0]; [_hideConstraint setConstant:0];
[_backgroundView setAlpha:1]; [_backgroundView setAlpha:1];
[_imagePickerView layoutIfNeeded]; [_imagePickerView layoutIfNeeded];
} }
completion:^(BOOL finished) { completion:^(BOOL finished) {
@ -479,20 +487,19 @@
[_imagePickerView setNeedsUpdateConstraints]; [_imagePickerView setNeedsUpdateConstraints];
[_imagePickerView updateConstraintsIfNeeded]; [_imagePickerView updateConstraintsIfNeeded];
_isVisible = NO;
if (animated) { if (animated) {
[UIView animateWithDuration:_animationTime [UIView animateWithDuration:_animationTime
delay:0.0 delay:0.0
usingSpringWithDamping:1 usingSpringWithDamping:1
initialSpringVelocity:0 initialSpringVelocity:0
options:0 options:0
animations:^{ animations:^{
[_hideConstraint setConstant:imagePickerHeight]; [_hideConstraint setConstant:imagePickerHeight];
[_backgroundView setAlpha:0]; [_backgroundView setAlpha:0];
[_imagePickerView layoutIfNeeded]; [_imagePickerView layoutIfNeeded];
} }
completion:^(BOOL finished) { completion:^(BOOL finished) {
@ -503,13 +510,13 @@
}]; }];
}]; }];
} else { } else {
[_hideConstraint setConstant:imagePickerHeight]; [_hideConstraint setConstant:imagePickerHeight];
[_backgroundView setAlpha:0]; [_backgroundView setAlpha:0];
[_imagePickerView layoutIfNeeded]; [_imagePickerView layoutIfNeeded];
[self dismissViewControllerAnimated:NO completion:^{ [self dismissViewControllerAnimated:NO completion:^{
if ([delegate respondsToSelector:@selector(imagePickerDidClose)]) { if ([delegate respondsToSelector:@selector(imagePickerDidClose)]) {
[delegate imagePickerDidClose]; [delegate imagePickerDidClose];
@ -520,13 +527,6 @@
} }
} }
- (void) dismissViewControllerAnimated:(BOOL)animated completion:(void (^ _Nullable)(void))completion {
[self dismissAnimated:animated];
if (completion) {
completion();
}
}
@end @end