我使用以下代码从相机或照片库中选择图像。在iOS 8中,它正在很好地选择图像。但是在iOS 9中。显示选择器视图但不选择图像。甚至没有回到控制器。点击图片什么都不做。我究竟做错了什么。
- (void)showImgaePickerViewForSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
self.imgPickerController = imagePickerController;
if (IS_IPAD) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:self.imgPickerController];
[popover presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2-200, self.view.frame.size.height/2 - 300, 400, 400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
self.popOver = popover;
}];
}else{
[self presentViewController:self.imgPickerController animated:YES completion:NULL];
}
}
答案 0 :(得分:2)
UIPopoverController
。相反,您应该UIViewController
使用modalPresentationStyle
设置为UIModalPresentationPopover
。
例如:
UIImagePickerController *imagePickerController = ...;
CGRect sourceRect = CGRectMake(self.view.frame.size.width/2-200, self.view.frame.size.height/2 - 300, 400, 400);
imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:imagePickerController animated:YES completion:nil];
imagePickerController.popoverPresentationController.sourceRect = sourceRect;
imagePickerController.popoverPresentationController.sourceView = self.view;
注意: UIModalPresentationPopover
是在iOS 8.0中引入的,如果您需要在8.0之前支持,那么您将需要一些条件检查来使用iOS 7及以上版本中的旧代码iOS 8 +。
注2:我相信上面的技术也足够智能,如果它应该模态地呈现选择器而不是iPhone上的弹出窗口(通过大小类),所以我不会这样做认为你需要进行IS_IPAD
检查。
答案 1 :(得分:2)
我发现我的应用在其他iPhone上运行良好(无论是iOS 8还是iOS 9)。我无法从任何其他应用程序中选择图像。然后我决定重置我的iPhone。现在一切都很完美。这是代码问题。这是我的iPhone问题。
答案 2 :(得分:1)
使用此代码,它在ios 9中正常工作。我正在使用操作表显示2个选项。
-(IBAction)imagepickertapped:(id)sender
{
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Take Photo" otherButtonTitles:@"Photo Library",nil];
popup.tag =909;
popup.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[popup showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet.tag ==909)
{
if (buttonIndex == 0)
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,kUTTypeImage,nil];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}
else
{
UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[altnot show];
}
} else if (buttonIndex == 1) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,kUTTypeImage,nil];
picker.delegate = self;
picker.editing = YES;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo{
[picker dismissViewControllerAnimated:YES completion:NULL];
imagedata = UIImageJPEGRepresentation(image, 0.3);
Userimage.image = image;
}
对于IPAD,请参阅此链接
答案 3 :(得分:0)
按照以下编码
- (void)imagePicker
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:pickerTakePhoto animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *imagePicked = info[UIImagePickerControllerEditedImage];
picker.delegate = self;
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
答案 4 :(得分:0)
由于不推荐使用操作表并且需要使用UIAlertController,我正在为新手的利益编写类似的答案:
- (IBAction)attachImageTapped:(id)sender
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}
}];
UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
}];
[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *imagePicked = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
答案 5 :(得分:0)
请按照以下步骤从图书馆中挑选图像,您可以使用“UIAlertController”从相机拍摄图像,只需在按钮点击事件中输入以下代码
- (IBAction)yourButtonClickEvent:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery =
[UIAlertAction actionWithTitle:@"Take a photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}
}];
UIAlertAction* takeAPicture =
[UIAlertAction actionWithTitle:@"Choose from gallery"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:NULL];
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
}];
[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:nil];
}
然后你把UIImagePicker委托方法就像这样:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *imagePicked = info[UIImagePickerControllerOriginalImage];
self.imgForUpaloadDocument.image = imagePicked;
[picker dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil);
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
答案 6 :(得分:0)
使用上面的代码我想添加一些你可能需要实现uiimage picker viewcontroller的东西。
如果您想阻止应用程序崩溃,您需要提供有关访问相机,照片库等的原因的说明。这是iOS10中的新功能。
在Info.plist文件中输入以下内容。
照片
密钥:隐私 - 照片库使用说明价值:$(PRODUCT_NAME)照片使用
相机
密钥:隐私 - 相机使用说明价值:$(PRODUCT_NAME)相机使用