如何将图像从Gallery加载到XCODE5中的UiImageView?

时间:2013-12-26 09:58:11

标签: iphone objective-c ios7 uiimageview uiimagepickercontroller

嗨我在Xcode 5中将图片从图库加载到UIImageView时出现问题。

当我尝试从UIImage中的图库中获取图像时,它会在我的屏幕中生成异常错误。

2 个答案:

答案 0 :(得分:4)

您可以使用以下方法从图库中加载图片。 单击图库按钮时,您必须致电methodToCallPhotos

-(void)methodToCallPhotos
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    [self presentModalViewController:imagePicker animated:YES];

}

此后,当您从图库中选择任何图像时。它调用如下的委托方法

-(void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
    imageView.image = image;
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

答案 1 :(得分:0)

首先检查您的应用是否被授予访问照片库的权限。

查看ALAssetsLibrary here的文档。要访问所有照片和视频,您需要枚举照片库中的所有组(专辑),然后枚举每组中的所有照片和图像。

ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];

[al enumerateGroupsWithTypes:ALAssetsGroupAll

    usingBlock:^(ALAssetsGroup *group, BOOL *stop)
    {
        [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
        {
            if (asset)
            {                
                 .. do something with the asset
            }
        }
    }

    failureBlock:^(NSError *error)
    {
        .. handle error 
    }
 ];