xcode UIImagePickerController 选取多个图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13093143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImagePickerController Pick Multiple images
提问by phil88530
I am trying to simply enable picking multiple images from photolibrary using the UIImagePickerController, I wish I can add a subview on bottom of the photo picker so it look something like this app does:
我试图简单地使用 UIImagePickerController 从照片库中选择多个图像,我希望我可以在照片选择器的底部添加一个子视图,这样它看起来像这个应用程序:
It there a simple way you can do it? My code currently only pops the images in a standard way but I only dismiss the controller when loaded 6 images
它有一个简单的方法可以做到吗?我的代码目前仅以标准方式弹出图像,但我仅在加载 6 个图像时关闭控制器
The important thing there is if anyway I can add a small view/toolbar to the photo picker view, like the example did, I then can do the rest
重要的是,无论如何我可以向照片选择器视图添加一个小视图/工具栏,就像示例一样,然后我可以完成其余的工作
- (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType{
//get all available source types
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
//if source type available and supported media type is not null
if ([UIImagePickerController isSourceTypeAvailable:sourceType
&& [mediaTypes count] > 0]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//picker.mediaTypes = mediaTypes; //allow videos
picker.delegate = self;
picker.sourceType = sourceType; //set source type to the given type
/** WANT TO ADD A CUSTOM VIEW IN THE PHOTO PICKER VIEW **/
[self presentViewController:picker animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error accessing media"
message:@"Device doesn't support that media type"
delegate:nil
cancelButtonTitle:@"Drat !"
otherButtonTitles: nil];
[alert show];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType]; //record media type
//if media type is image
if ([lastChosenMediaType isEqual:(NSString *) kUTTypeImage]) {
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerOriginalImage]; //load image
//save the image if is from camera shot
if (imageOrVideoSourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum (chosenImage, nil, nil , nil);
}
[images addObject:chosenImage]; //add to image list
imageNum++;
}
changeImageOrVideo = true;
if(imageNum >= 5){
[picker dismissViewControllerAnimated:YES completion:nil];
}
}
回答by EarlyRiser
The main reason for using hacks like shifting the UIImagePickerController up and showing selected images underneath was because the asset library alternative would involve the user being asked for location access due to the information about where the photo was taken being available in the image metadata.
使用像上移 UIImagePickerController 并在下方显示选定图像这样的技巧的主要原因是,资产库替代方案将涉及要求用户访问位置,因为图像元数据中提供了有关照片拍摄地点的信息。
In iOS 6 the user is asked if they want to allow the app to access their photos (not location) and you get asked this question for both the asset library approach and the UIImagePickerController approach.
在 iOS 6 中,系统会询问用户是否允许应用访问他们的照片(而不是位置),并且您会针对资产库方法和 UIImagePickerController 方法询问这个问题。
As such I think that hacks like the above are nearing the end of their usefulness. Here is a link to a library providing selection of multiple images using the Assets librarythere are others.
因此,我认为像上面这样的 hack 即将结束它们的用处。这是一个库的链接,该库使用资产库提供了多个图像的选择,还有其他库。