I am trying to simply enable picking multiple images from photolibrary using the UIImagePickerController
.I'm relatively new to XCode
and I don't understand how to allow the user to pick multiple images from the UIImagePickerControler
. This is my current code.Please help any body how to pick multiple images from UIImagePickerController
.
我尝试使用UIImagePickerController从photolibrary中选择多个图像。我对XCode比较陌生,我不知道如何让用户从UIImagePickerControler中选择多个图像。这是我当前的代码。请帮助任何身体如何从UIImagePickerController选择多个图像。
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch(buttonIndex)
{
case 0:
[self takeNewPhotoFromCamera];
break;
case 1:
[self choosePhotoFromExistingImages];
default:
break;
}
}
- (void)takeNewPhotoFromCamera
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
}
-(void)choosePhotoFromExistingImages
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypePhotoLibrary];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
}
3 个解决方案
#1
7
With UIImagePickerController
you are able to get only one picture. If you need to pick more you need a custom image picker, such as ELCImagePickerController. It works well! You can download it here.
使用UIImagePickerController你只能得到一张图片。如果需要选择更多,则需要自定义图像选择器,如ELCImagePickerController。它工作得很好!你可以在这里下载。
#2
6
As all said above, it is not possible using just ImagePickerController. You need to do it custom. Apple recently introduced PHASSET Library which makes this easy. There is a sample code also in the developer library. I am laying down the steps here.
如上所述,仅使用ImagePickerController是不可能的。你需要定制它。苹果公司最近推出了PHASSET Library,这让这很容易。在developer库中也有一个示例代码。我在这里放*阶。
- Setup your own collection view
- 设置自己的集合视图
- Load the collection view with pictures from gallery (using PHAsset, explained below)
- 使用来自gallery的图片加载集合视图(使用PHAsset,在下面解释)
- Show each of the picture in your cellForItemAtIndexPath (using PHAsset, explained below)
- 在cellForItemAtIndexPath中显示每个图片(使用PHAsset,解释如下)
- In your didSelectItemAtIndexPath, keep track of which pictures were selected and add a tick mark image. Add it to a local array
- 在didSelectItemAtIndexPath中,跟踪选择了哪些图片,并添加一个标记图像。将它添加到本地数组中
- When done, read from the picture array and process
- 完成后,从图像数组和过程中读取。
Snippet code for Loading Images from gallery.
用于从gallery加载图像的代码片段。
// Create a PHFetchResult object for each section in the table view.
@property (strong, nonatomic) PHFetchResult *allPhotos;
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
if ( _isVideo == YES){
_allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:allPhotosOptions];
}
else {
//_allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
_allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];
}
You now get all the images in your _allPhotos array which you will use like below in the cellForItemAtIndexPath
现在,您将获得_allPhotos数组中的所有图像,如下面的cellForItemAtIndexPath所示
PHAsset *asset = self.allPhotos[indexPath.item];
//cell.representedAssetIdentifier = asset.localIdentifier;
cell.selectedTick.hidden = YES;
cell.isSelected = NO;
// Request an image for the asset from the PHCachingImageManager.
[self.imageManager requestImageForAsset:asset
targetSize:CGSizeMake(100, 100)
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
cell.photo.image = result;
}];
return cell;
Thatz it. Hope this helps.
Thatz它。希望这个有帮助。
#3
0
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.
在ios6中,用户被问到是否允许应用程序访问他们的照片(而不是位置),你会被问到这个问题,因为资产库方法和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 library there are others.
就这一点而言,我认为像上面这样的黑客正接近其用途的尽头。这里有一个指向库的链接,该库使用资产库提供多个映像的选择。
Happy Coding!!!
编码快乐! ! !
#1
7
With UIImagePickerController
you are able to get only one picture. If you need to pick more you need a custom image picker, such as ELCImagePickerController. It works well! You can download it here.
使用UIImagePickerController你只能得到一张图片。如果需要选择更多,则需要自定义图像选择器,如ELCImagePickerController。它工作得很好!你可以在这里下载。
#2
6
As all said above, it is not possible using just ImagePickerController. You need to do it custom. Apple recently introduced PHASSET Library which makes this easy. There is a sample code also in the developer library. I am laying down the steps here.
如上所述,仅使用ImagePickerController是不可能的。你需要定制它。苹果公司最近推出了PHASSET Library,这让这很容易。在developer库中也有一个示例代码。我在这里放*阶。
- Setup your own collection view
- 设置自己的集合视图
- Load the collection view with pictures from gallery (using PHAsset, explained below)
- 使用来自gallery的图片加载集合视图(使用PHAsset,在下面解释)
- Show each of the picture in your cellForItemAtIndexPath (using PHAsset, explained below)
- 在cellForItemAtIndexPath中显示每个图片(使用PHAsset,解释如下)
- In your didSelectItemAtIndexPath, keep track of which pictures were selected and add a tick mark image. Add it to a local array
- 在didSelectItemAtIndexPath中,跟踪选择了哪些图片,并添加一个标记图像。将它添加到本地数组中
- When done, read from the picture array and process
- 完成后,从图像数组和过程中读取。
Snippet code for Loading Images from gallery.
用于从gallery加载图像的代码片段。
// Create a PHFetchResult object for each section in the table view.
@property (strong, nonatomic) PHFetchResult *allPhotos;
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
if ( _isVideo == YES){
_allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:allPhotosOptions];
}
else {
//_allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
_allPhotos = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];
}
You now get all the images in your _allPhotos array which you will use like below in the cellForItemAtIndexPath
现在,您将获得_allPhotos数组中的所有图像,如下面的cellForItemAtIndexPath所示
PHAsset *asset = self.allPhotos[indexPath.item];
//cell.representedAssetIdentifier = asset.localIdentifier;
cell.selectedTick.hidden = YES;
cell.isSelected = NO;
// Request an image for the asset from the PHCachingImageManager.
[self.imageManager requestImageForAsset:asset
targetSize:CGSizeMake(100, 100)
contentMode:PHImageContentModeAspectFill
options:nil
resultHandler:^(UIImage *result, NSDictionary *info) {
cell.photo.image = result;
}];
return cell;
Thatz it. Hope this helps.
Thatz它。希望这个有帮助。
#3
0
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.
在ios6中,用户被问到是否允许应用程序访问他们的照片(而不是位置),你会被问到这个问题,因为资产库方法和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 library there are others.
就这一点而言,我认为像上面这样的黑客正接近其用途的尽头。这里有一个指向库的链接,该库使用资产库提供多个映像的选择。
Happy Coding!!!
编码快乐! ! !