程序员日常ios实用开发技巧

iOS相册选取回调的一个问题

2017-12-07  本文已影响4人  听风轻咛

问题环境:Xcode 8.1 + iOS SDK10.1
首先是调用之前的iOS10权根问题,具体问题见:
“This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription(NSPhotoLibraryUsageDescription) key with a string value explaining to the user how the app uses this data.”

以上问题调用相机和调用相册都会遇到,分别在Info.plist中添加Dictionary:
NSCameraUsageDescription(相机),
NSPhotoLibraryUsageDescription(相册)

第二个先上代码,我们调用相册的通用代码:

UIImagePickerController* _imagePickerController;
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.delegate = self;
_imagePickerController.allowsEditing = YES;
_imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_imagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:_imagePickerController.sourceType];
_imagePickerController.allowsEditing = YES;
[self presentViewController:_imagePickerController animated:YES completion:nil];

有一个回调方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {

}

想说的是这个方法已经被废弃,调用不会产生任务效果,刚开始还纠结了好久;这里要说一下废弃的问题:

- (void) application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder NS_AVAILABLE_IOS(6_0);
-     @property(nonatomic,getter=isProximitySensingEnabled) BOOL proximitySensingEnabled NS_DEPRECATED_IOS(2_0, 3_0); 

这里的NS_AVAILABLE_IOS(6_0)NS_DEPRECATED_IOS(2_0, 3_0)

NS_AVAILABEL_IOS(6_0)表示,自IOS6.0开始支持该方法,若在IOS6.0之前的版本使用该函数,则会导致 Crash;

NS_DEPRECATED_IOS(2_0,3_0)表示该函数只能在IOS2.0 和 IOS3.0之间使用,是已被废弃的函数,但并不是说在IOS3.0之后不能使用该函数,是可以使用,但也需要考虑找其他替代方法了;现在看来,旧方法不适宜用,因为某些可以苹果已经不再用了;(为什么不用保险的呢?)

另外,还有 NS_DEPRECATED(10_6,10_9,2_0,7_0)这种写法,为自mac10.6和ios2.0引入,在mac10.9和ios7.0被废弃

另外,iOS默认不添加回调的时候相册图片选取或者相册中点取消是返回原先界面的;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    NSLog(@"选择完毕-----info:%@",info);
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

接下来就是这两个回调了,一个选取,一个取消,跳回原来界面的操作需要添加,因为如果实现了这两个方法,就是处理自己的逻辑了;

上一篇下一篇

猜你喜欢

热点阅读