- 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];
if (!self.didUpdateConstraints) {
self.didUpdateConstraints = YES;
[self.contentView autoPinEdgesToSuperviewEdges];
[_photoImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:8];
@ -64,6 +62,8 @@
[_loadingSpinner autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[_loadingSpinner autoAlignAxisToSuperviewAxis:ALAxisVertical];
self.didUpdateConstraints = YES;
}
}

View File

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