0

我可以使用以下代码访问 iPhone 照片库。我需要添加什么才能访问 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];    
}
}

谢谢

4

2 回答 2

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....
}
于 2012-09-06T19:47:16.960 回答
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;
    }
}
于 2013-09-21T16:51:18.190 回答