为什么我不能访问iPad照片库?

时间:2013-06-14 08:46:16

标签: ios ipad uiimagepickercontroller uipopovercontroller

我正在使用IImagePickerController从照片库中获取照片。适用于iPhone,但无法在iPad上运行。有建议使用UIpopoverController的解决方案。我在任何早期版本中使用UIpopoverController,我无法使其工作。继续收到popoverController不存在的错误。以下是我的代码。是否有允许您使用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];
}

2 个答案:

答案 0 :(得分:2)

在iPad上,您需要在UIImagePickerController中显示UIPopoverController

您无法使用presentViewController:

直接显示它

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

Reference 参考: 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. UIImagePickerController in iPad

答案 1 :(得分: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];



    }