Ios 如何在代码中制作图像选择器
如何在代码中创建图像选择器 我使用iOS 6.0和ARC 我希望能够选择图片并以某种方式获取所选图像的UIImage。要显示它:Ios 如何在代码中制作图像选择器,ios,objective-c,image,Ios,Objective C,Image,如何在代码中创建图像选择器 我使用iOS 6.0和ARC 我希望能够选择图片并以某种方式获取所选图像的UIImage。要显示它: UIImagePickerController *picker; picker = [[UIImagePickerController alloc] init]; [picker setDelegate:self]; [picker setSourceType:type]; [self presentViewController:picker animated:YE
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);
}
}
在.h
文件的@interface ViewController
行中添加代理
如果要将模拟器用作调试器,请确保模拟器中有PIC
然后调用以下方法
- (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];
}
基本的实例化如下所示
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的简单教程
为了增加这一点,您需要实现
UIImagePickerControllerDelegate
,其中包括在.h文件中声明。您好,我了解到,此错误在该设备上不可用。2013-01-22 21:37:27.745 Grapher2[52960:11303]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“在iPad上,UIImagePickerController必须通过UIPopoverController显示”,这是崩溃的线路[self-presentModalViewController:imagePicker动画:是];你没有指定你在iPad上。这段代码适用于iPhone。你应该看一下UIPopoverController的文档,或者就这个主题发表一个单独的问题。谢谢,我知道它不一样。谢谢,我想这是错的!这是一个普遍的问题,我们都可以得到!