先来看最终效果
在OC中的使用方式
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage,nil];
picker.allowsEditing = YES; // 允许简单编辑图片
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
在Swift中的使用方式
错误方式
,模仿OC的写法
picker.mediaTypes = [kUTTypeImage as String]
正确方式
picker.mediaTypes = [UTType.image.identifier]
实例
在OC使用TZImagePickerController第三方库,这个在网上能找到很多资料,但在Swift中如何使用TZImagePickerController相关资料还是比较匮乏,下面实例,就在Swift中,如何使用TZImagePickerController这个库,来从相册中选取图片,及拍照获取图片
- 初始化TZImagePickerController
lazy var imagePickerVc: UIImagePickerController = {
let pickerVc = UIImagePickerController()
pickerVc.delegate = self
pickerVc.navigationBar.barTintColor = self.navigationController?.navigationBar.barTintColor
pickerVc.navigationBar.tintColor = self.navigationController?.navigationBar.tintColor
let tzBarItem = UIBarButtonItem.appearance(whenContainedInInstancesOf: [TZImagePickerController.self])
let barItem = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIImagePickerController.self])
let titleTextAttributes = tzBarItem.titleTextAttributes(for: .normal)
barItem.setTitleTextAttributes(titleTextAttributes, for: .normal)
return pickerVc
}()
- 从相册中选取图片
let tzImagePickerVC = TZImagePickerController(maxImagesCount: 1, columnNumber: 1, delegate: nil, pushPhotoPickerVc: true)
tzImagePickerVC?.allowTakeVideo = false
tzImagePickerVC?.allowTakePicture = false
tzImagePickerVC?.allowPickingVideo = false
tzImagePickerVC?.modalPresentationStyle = .fullScreen
// tzImagePickerVC?.preferredLanguage = "zh-Hans"
tzImagePickerVC?.allowPreview = false
self.present(tzImagePickerVC!, animated: true)
///从相册中选取图片完成后的回调
tzImagePickerVC!.didFinishPickingPhotosHandle = { photos,assets,isSelectOriginalPhoto in
print("----")
}
- 真机拍照
private func takePhoto() {
let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
if authStatus == AVAuthorizationStatus.restricted || authStatus == AVAuthorizationStatus.denied {
let message = """
请在iPhone的"设置-隐私-相机"中允许访问相机
"""
let alertController = UIAlertController(title: "无法使用相机", message: message, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel)
let settingAction = UIAlertAction(title: "设置", style: .default ){_ in
UIApplication.shared.open(NSURL(string: UIApplication.openSettingsURLString)! as URL)
}
alertController.addAction(cancelAction)
alertController.addAction(settingAction)
present(alertController, animated: true)
} else if authStatus == AVAuthorizationStatus.notDetermined {
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
DispatchQueue.main.sync {
self.takePhoto()
}
}
}
} else if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.denied {
let message = """
请在iPhone的"设置-隐私-相册"中允许访问相册
"""
let alertController = UIAlertController(title: "无法访问相册", message: message, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel)
let settingAction = UIAlertAction(title: "设置", style: .default ){_ in
UIApplication.shared.open(NSURL(string: UIApplication.openSettingsURLString)! as URL)
}
alertController.addAction(cancelAction)
alertController.addAction(settingAction)
present(alertController, animated: true)
} else if PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.notDetermined {
TZImageManager.default().requestAuthorization {
self.takePhoto()
}
} else {
// TZLocationManager.default().startLocation { locations in
// self.location = locations?.first
// } failureBlock: { error in
// self.location = nil
// }
let sourceType = UIImagePickerController.SourceType.camera
self.imagePickerVc.sourceType = sourceType
self.imagePickerVc.mediaTypes = [UTType.image.identifier]
present(self.imagePickerVc, animated: true)
}
}
///拍照完成后的回调
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
print("打印一下》》》》》")
picker.dismiss(animated: true)
let type = info[.mediaType]
let image = info[.originalImage] as! UIImage
print("打印一下》》》》》type=\(String(describing: type))")
}
结尾
今天iOS 相关技术的分享就到这里喽,小伴们,觉得有点用的话,或者已经看到这里面来的请点个赞加关注吧~~ 后续分享更多有关iOS的文章。如果有疑问的话,请在下方留言~