使用系统相册、拍照、iCloud功能
1、添加权限:
相机权限 Privacy - Camera Usage Description
App想要访问您手机的系统相机,是否允许?
相册权限 Privacy - Photo Library Additions Usage Description
App想要访问您手机的相册,是否允许?
2、系统库头文件
#import <Photos/Photos.h>
3、点击按钮,弹出选择弹窗
- (void)inavBtnClicked:(UIButton*)btn
{
__weak typeof(self)weakSelf = self;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"上传" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
__strong typeof(self)strongSelf = self;
[strongSelf openPhotosAlbum];
}];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
__strong typeof(self)strongSelf = self;
[strongSelf openCamera];
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"iCloud" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
__strong typeof(self)strongSelf = self;
[strongSelf openICloud];
}];
[alert addAction:action];
[alert addAction:action1];
[alert addAction:action2];
[weakSelf presentViewController:alert animated:YES completion:^{
}];
}
如图:
4、实现相关方法
- (void)openPhotosAlbum {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//如果需要编辑图片,建议sourceType选择UIImagePickerControllerSourceTypeSavedPhotosAlbum,如果不需要,可选择UIImagePickerControllerSourceTypePhotoLibrary。
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)openCamera {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)openICloud {
if (![ViewController ICloudEnable]) {
NSLog(@"iCloud没有开启");
return;
}
NSArray *documentTypes = @[@"public.content",
@"public.text",
@"public.source-code",
@"public.image",
@"public.jpeg",
@"public.png",
@"com.adobe.pdf",
@"com.apple.keynote.key",
@"com.microsoft.word.doc",
@"com.microsoft.excel.xls",
@"com.microsoft.powerpoint.ppt"];
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
//判断iCloud是否可用
+ (BOOL)ICloudEnable {
NSURL *url = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
return url != nil;
}
5、代理
#pragma mark - UIImagePickerControllerDelegate
//selected
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//此处需要先dismiss掉picker,然后再present出alert,佛否则alert显示会出bug
[picker dismissViewControllerAnimated:YES completion:nil];
//获取经过编辑后的图片
UIImage *image = info[UIImagePickerControllerEditedImage];
if (!image) {
//如果未编辑,取原图
image = info[UIImagePickerControllerOriginalImage];
}
if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum)
{
NSURL *url = nil;
if (@available(iOS 11.0, *)) {
url = info[UIImagePickerControllerImageURL];
} else {
url = info[UIImagePickerControllerReferenceURL];
}
}
else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
__block NSString *locolId = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//保存到相册
PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
locolId = request.placeholderForCreatedAsset.localIdentifier;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (error == nil) {
//获取图片信息
PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[locolId] options:nil];
PHAsset *asset = [result firstObject];
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
//NSURL *url = info[@"PHImageFileURLKey"];
}];
}
else {
NSLog(@"图片保存失败!");
}
}];
}
}
//cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls
{
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
{
[controller dismissViewControllerAnimated:YES completion:nil];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
{
}
PHPhotoLibrary提供了拍照保存到相册等功能。