如何在iphone中的应用程序中访问内置相册

时间:2011-09-08 12:36:14

标签: iphone

  

可能重复:
  Access Photo Album from iPhone Code

我是iPhone开发的新手。我正在开发一个应用程序,我需要查看我的iPhone上的照片。谁能告诉我如何实现这个目标?

由于

Puneet Garg

5 个答案:

答案 0 :(得分:1)

您有多种可能性:

  • 如果要显示允许用户从相册中选取一张照片的标准视图,请使用UIImagePickerController。如果您只需要用户从其相册中选择现有的照片,这很容易使用并且很常见。 - > see here

  • 如果您想以编程方式操作PhotoAlbum中的所有照片,请自定义显示缩略图的视图,或以其他方式列出照片并获取有关每张图像的更详细信息,循环播放照片中的所有图像相册等使用AssetsLibraryALAssetALAssetsLibrary,...和类似的类) - > see here

答案 1 :(得分:0)

您必须使用UIImagePickerController ...遵循此Link

答案 2 :(得分:0)

- (void)viewDidLoad
{
    [super viewDidLoad];
    [activity startAnimating];


    appObj=(ImagePickerAppDelegate *)[[UIApplication sharedApplication]delegate];

    void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
    {
        if(result != NULL) 
        {
            //assets is a mutualable array...for storing the images that are in the device..
            [assets addObject:result];
        }
    };

    void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) 
    {
        if(group != nil)
        {
            [group enumerateAssetsUsingBlock:assetEnumerator];
        }
        //meth is a user defined method..   
        [self meth];
        [activity stopAnimating];
        [activity setHidden:YES];
    };
    assets = [[NSMutableArray alloc] init];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetGroupEnumerator 
                         failureBlock: ^(NSError *error) { NSLog(@"Failure");}];
}


-(void)meth
{
    NSLog(@"%i",[assets count]);

    if(userOrientation==UIInterfaceOrientationPortrait || userOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        NSLog(@"haii");
        [scrollView removeFromSuperview];

        scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
        scrollView.backgroundColor=[UIColor whiteColor];

        NSLog(@"%i",[assets count]);
        for (int i = 0; i < [assets count]; i++) 
        {
            imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [imgBtn setFrame:CGRectMake((i%4*80)+2,(i/4*80)+2,75,75)];
            imgBtn.tag=i;
            [imgBtn addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
            ALAsset *asset=[assets objectAtIndex:i];
            [imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
            [scrollView addSubview:imgBtn];
        }
        scrollView.contentSize = CGSizeMake(320,(([assets count]/4)+1)*300 );
    }

    if(userOrientation==UIInterfaceOrientationLandscapeRight || userOrientation==UIInterfaceOrientationLandscapeLeft)
    {
        [scrollView removeFromSuperview];
        scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 480,320)];
        for (int i = 0; i < [assets count]; i++) 
        {
            imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [imgBtn setFrame:CGRectMake((i%6*80)+2,(i/6*80)+2,75,75)];
            imgBtn.tag=i;
            [imgBtn addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
            ALAsset *asset=[assets objectAtIndex:i];
            [imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
            [scrollView addSubview:imgBtn];
        }
        scrollView.contentSize = CGSizeMake(480,(([assets count]/4)+1)*300);
    }
    [self.view addSubview:scrollView];
}

-(void)imageClicked:(UIButton *)sender
{
    //for picking the images that the user has selected we are using other array "selectedImages" i.e declared in the app delegate
    ALAsset *asset=[assets objectAtIndex:sender.tag];
    [appObj.selectedImages addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
    NSLog(@"%i",[appObj.selectedImages count]);
    [self.navigationController popViewControllerAnimated:YES ];
}

答案 3 :(得分:0)

使用UIImagePickerController。这里有一个很好的教程。

http://www.zimbio.com/iPhone/articles/1109/Picking+Images+iPhone+SDK+UIImagePickerController

答案 4 :(得分:0)

您可以使用以下代码访问相册。

UIImagePickerController * anImagePickerController = [[UIImagePickerController alloc] init];
anImagePickerController.delegate = self;
anImagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
anImagePickerController.showsCameraControls = YES;
[self presentModalViewController:anImagePickerController animated:NO];
[anImagePickerController release];