使用Xcode访问iPad上的照片库

时间:2012-09-06 19:13:31

标签: iphone ipad camera xcode4.3

我可以使用以下代码访问iPhone Photo库。我需要添加什么才能访问iPad上的照片库。我知道它与UIPopOver有关,但不知道如何实现它。

- (void) useCamera
{
if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              nil];
    imagePicker.allowsEditing = NO;
    [self presentModalViewController:imagePicker
                            animated:YES];

    newMedia = YES;
}
}


- (void) useCameraRoll
{
if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              nil];
    imagePicker.allowsEditing = NO;
    [self presentModalViewController:imagePicker animated:YES];

    newMedia = NO;
}
}

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info
                       objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = [info
                      objectForKey:UIImagePickerControllerOriginalImage];

    imageView.image = image;
    if (newMedia)
        UIImageWriteToSavedPhotosAlbum(image,
                                       self,

@selector(image:finishedSavingWithError:contextInfo:),
                                       nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}
}

-(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];    
}
}

由于

2 个答案:

答案 0 :(得分:0)

以下是一些代码,它适用于支持iPhone和iPad的现有应用程序:

注意:SSiOSUtilities是我创建的一个类,其中包含用于确定您是在iPad还是iPhone上的代码。

- (IBAction)fromCamera:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    [picker setDelegate:self];

    [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [picker setAllowsEditing:YES];

    if ([SSiOSUtilities isIPad])
    {
        UIPopoverController *tempPopOver = [[UIPopoverController alloc] initWithContentViewController:picker];
        [tempPopOver presentPopoverFromRect:[[self cameraButton] frame] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        [self setPopOver:tempPopOver];
    }
    else
    {
        [self presentModalViewController:picker animated:YES];
    }

    [picker release];
}

- (IBAction)fromLibrary:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    [picker setDelegate:self];

    [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [picker setAllowsEditing:YES];

    if ([SSiOSUtilities isIPad])
    {
        UIPopoverController *tempPopOver = [[UIPopoverController alloc] initWithContentViewController:picker];
        [tempPopOver presentPopoverFromRect:[[self libraryButton] frame] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        [self setPopOver:tempPopOver];
    }
    else
    {
        [self presentModalViewController:picker animated:YES];
    }

    [picker release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    if ([SSiOSUtilities isIPad])
    {
        [[self popOver] dismissPopoverAnimated:YES];
        [[self popOver] release];
    }

    [picker dismissModalViewControllerAnimated:YES];

    // rest of code for image here....
}

答案 1 :(得分:0)

这是我使用的一些代码。它创建一个UIImagePickerController,然后将其用作UIPopoverController的内容视图控制器。变量vc在.h中声明,是一个UIPopoverController。

注意:iOS 7刚刚问世,我发现当我运行此代码时,屏幕会略微变暗,就像显示照片库一样,但是弹出控制器没有显示出来。

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
    if (vc == nil)  //show the popover if it is not being displayed
    {
        imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        vc = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
        [vc presentPopoverFromBarButtonItem:chooseButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
    else    //hide the popover if it is already being displayed
    {
        [vc dismissPopoverAnimated:YES];
        vc = nil;
    }
}