-1

I'm using IImagePickerController to get photos from the photo library. Works great on the iphone, but won't work on an iPad. There were solutions suggested to use UIpopoverController. I was using UIpopoverController in any earlier version, and I couldn't get it to work. Keep getting an error that the popoverController did not exist. Below is my code. Is there a framework that allows you to use UIPopoverController?

- (IBAction)getPhoto4:(id)sender {

    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        UIImagePickerController *imagePicker =[[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = @[(NSString *) kUTTypeImage ,(NSString *)kUTTypeMovie];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = 320.0/480.0;

    if(imgRatio!=maxRatio){
        if(imgRatio < maxRatio){
            imgRatio = 480.0 / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = 480.0;
        }
        else{
            imgRatio = 320.0 / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = 320.0;
        }
    }
    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Get the data for the image
    NSData* imageData = UIImageJPEGRepresentation(img, 1.0);

    // Give a name to the file
    IMAGE_COUNTER = IMAGE_COUNTER + 1;
    NSString* incrementedImgStr = [NSString stringWithFormat: @"zTempDataFilePleaseDelete%d.jpg", IMAGE_COUNTER];

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

    // Now we get the full path to the file
    NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr];

    if(IMAGE_COUNTER == 4) {
        imageView4.image = img;
        photoHeight4 = img.size.height;
        photoWidth4 = img.size.width;
        photoPath4 = fullPathToFile2;
    }

    // and then we write it out
    [imageData writeToFile:fullPathToFile2 atomically:NO];

}

-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
 contextInfo:(void *)contextInfo
{
    if (error) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle: @"Save failed"
                              message: @"Failed to save image"
                              delegate: nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker1
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
4

2 回答 2

2

在 iPad 上,您需要显示UIImagePickerController嵌入在UIPopoverController.

您不能使用直接显示它presentViewController:

如果您正在显示相机,那么您可以使用 presentViewController,否则您需要使用弹出框在 iPad 中显示图像选择器。

参考 参考: UIImagePickerController


所以而不是:

[self presentViewController:imagePicker animated:YES completion:nil];

利用:

UIPopoverController  *photoPop = [[UIPopoverController alloc]initWithContentViewController:imagePicker];
[photoPop presentPopoverFromRect:CGRectMake(100,100,300,300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

检查这些教程:

  1. UIImagePickerController
  2. iPad 中的 UIImagePickerController
于 2013-06-14T08:58:34.797 回答
0

试试这个代码:在 .h 文件中实现委托

UIImagePickerControllerDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate

在 .m 文件中:

@implementation UIImagePickerController(Nonrotating)

- (BOOL)shouldAutorotate {
    return NO;
}
@end
@implementation UINavigationController (Rotation_IOS6)


-(NSUInteger) supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}
-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

-(void)photoclick
{
    UIImagePickerController *pckrImage=[[UIImagePickerController alloc]init];
            pckrImage.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            pckrImage.allowsEditing = YES;
            pckrImage.delegate=self;
           // UITableViewCell *cell = [tblIncident cellForRowAtIndexPath:[tblIncident indexPathForSelectedRow]];


         UIPopoverController  * poppickePhoto = [[UIPopoverController alloc]initWithContentViewController:pckrImage];

            poppickePhoto.delegate = self;
           // poppickePhoto.popoverContentSize =CGSizeMake(320,480);
            [poppickePhoto presentPopoverFromRect:yourbtn.frame inView:yourbtn.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

更新

  - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


        UIImage *choosedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        [picker dismissViewControllerAnimated:YES completion:NULL];

        yourumageview.image=choosedImage;
        [poppickePhoto dismissPopoverAnimated:YES];



    }
于 2013-06-14T09:03:01.840 回答