如何将照片从库上传到应用程序

时间:2013-09-24 08:41:41

标签: objective-c uiimagepickercontroller

我正在尝试将上传图片功能添加到我的iphone应用程序,当用户访问他们的主页时,他们可以从手机上传照片,下次他们访问该照片时将显示该照片。

我有什么:

//Button action to add upload function

-(IBAction)upload:(id)sender
{
    UIImagePickerController *pics = [[UIImagePickerController alloc] init];
    pics.delegate = self;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        pics.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    [self presentViewController:pics animated:YES completion:NULL];
    [pics release];
}

- (void)imagePickerController:(UIImagePickerController *) Picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // replace image holder with chosen image
    _imageView1.image=UIImagePickerControllerSourceTypePhotoLibrary;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *) Picker
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

2 个答案:

答案 0 :(得分:1)

UIImagePickerControllerSourceTypePhotoLibrary引用整个照片库,让用户选择相册。

didFinishPickingMediaWithInfo方法中,使用

_imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage];

可选:如何访问设备的照片库?我基本上是使用 AssetsLibrary.framework 来实现的。它为我提供了更多选项玩它。

查看github中提供的示例代码。它提供了选择多个图像的功能。它与UIImagePickerController一样易于设置,并且可以纵向和横向两种方式工作。它需要添加AssetsLibrary.framework。此代码使用ARC。

答案 1 :(得分:0)

试试这个,

- (void)imagePickerController:(UIImagePickerController *) Picker

    didFinishPickingMediaWithInfo:(NSDictionary *)info {

//replace image holder with chosen image 

 UIImage *pickedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[_imageView1 setImage: pickedImage];

}