Source Code.
Changed to update UI on main thread.
ex)
dispatch
sync(dispatchget
mainqueue(),^{
//UI update process
});
[Camera Roll]
(IBAction)onTapPhotoLibraryBtn:(id)sender {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusAuthorized: {
// ユーザーが、アプリが写真のデータへアクセスすることを許可している
//
PHAssetを取得
PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
if (allPhotosResult.count == 0) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:@"カメラロールに写真がありません。"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
dispatch
sync(dispatchget
mainqueue(), ^{ //20200713
imagePickerController = [QBImagePickerController new];
imagePickerController.delegate = self;
imagePickerController.allowsMultipleSelection = YES;
// imagePickerController.maximumNumberOfSelection
= 6;
imagePickerController.showsNumberOfSelectedAssets = NO;
// imagePickerController.mediaType =
QBImagePickerMediaTypeImage;
imagePickerController.showsNumberOfSelectedAssets = NO;
imagePickerController.assetCollectionSubtypes = @[
@(PHAssetCollectionSubtypeSmartAlbumUserLibrary)
];
imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen; //20200713
//
imagePickerController.modalInPopover = YES;
//20200713
[self presentViewController:
imagePickerController animated:YES completion:NULL];
});
//20200713
break;
}
case PHAuthorizationStatusRestricted: {
//
PhotoLibraryへのアクセスが許可されていない時
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"アプリ使用にはカメラへのアクセス許可が必要になります。iPhoneの「設定」機能から許可設定をおこなってください。" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// appの設定画面へ遷移
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}]];
[self presentViewController:alertController animated:YES completion:nil];
break;
}
case PHAuthorizationStatusDenied: {
// ユーザーが明示的に、アプリが写真のデータへアクセスすることを拒否した時
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"アプリ使用にはカメラへのアクセス許可が必要になります。iPhoneの「設定」機能から許可設定をおこなってください。" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// appの設定画面へ遷移
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}]];
[self presentViewController:alertController animated:YES completion:nil];
break;
}
default: {
PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
if (allPhotosResult.count == 0) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@""
message:@"カメラロールに写真がありません。"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
break;
}
}
}];
}
[Camera] // カメラの利用
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
// カメラが利用可能かチェック
if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
dispatchsync(dispatch
getmain
queue(), ^{ //20200713
// インスタンスの作成
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
// カメラから画像を取り込む設定にする
cameraPicker.sourceType = sourceType;
//UIImagePickerControllerDelegate
のデリゲートになる
cameraPicker.delegate = self;
// 撮影後に写真の編集を行うかどうか
cameraPicker.allowsEditing = YES;
//モーダルビューでカメラ起動
[self presentViewController:cameraPicker animated:YES completion:nil];
});
//20200713
}
else{
//
label.text = @"error";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"シミュレータではカメラは起動できません"
message:@"撮影できません"
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
return;
}
}