如何在代码中创建图像选择器?
我使用iOS 6.0,使用ARC。
我希望能够选择图片并以某种方式获得所选图像的UIImage。
答案 0 :(得分:3)
基本实例化就像这样
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];
imagePicker.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];
有三种获取图像的方法
//Pick from Camera
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//Make sure to enable permission for this in info.plist; see: https://stackoverflow.com/a/44690117/2057171
//Pick from all folders in the gallery
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//Pick from PhotosAlbum(camera roll)
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
然后检查源
的存在//Check Camera available or not
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
//Check PhotoLibrary available or not
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
//Check front Camera available or not
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront])
您应该实现委托协议以获取用户选择的图像
//Tells the delegate that the user picked a still image or movie.
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[imagepicker dismissModalViewControllerAnimated:YES];
}
//Tells the delegate that the user cancelled the pick operation.
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
}
使用UIImagePickerController的简单教程
答案 1 :(得分:1)
<UIImagePickerControllerDelegate>
将代理人添加到@interface ViewController
文件中的.h
行
如果您打算使用模拟器作为调试器,请确保您的模拟器中有图片 然后调用以下方法
- (void)showImagePicker
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentModalViewController:imagePicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the selected image.
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
答案 2 :(得分:0)
呈现它:
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[picker setSourceType:type];
[self presentViewController:picker animated:YES completion:nil];
获取UIImage:
- (void)imagePickerController:(UIImagePickerController *)thePicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Do something with "image"
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// users typically expect that if they took a photo it will be saved
if (thePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
}