如何使用ALAssetsLibrary在现有图像中选择路径资产?

时间:2014-04-24 20:43:49

标签: objective-c alassetslibrary

我有这个代码在iOS照片库中编写/创建一个新图像:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    // Request to save the image to camera roll
    [library writeImageToSavedPhotosAlbum:[imagem CGImage] orientation:(ALAssetOrientation)[imagem imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){

//code here...

}

执行此操作的代码可能标记为代码 writeImageToSavedPhotosAlbum ,但在这段时间我不需要在库中创建新图像,我需要获取图像的路径存在于库中,在我的例子中,图像在变量'imagem'中。

我可以用什么代码更改 writeImageToSavedPhotosAlbum 来获取图片路径?

[编辑]

我以这种格式获取变量imagem中的图像:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    UIImage *imagem  = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imagePicker dismissViewControllerAnimated:YES completion:nil];
}

2 个答案:

答案 0 :(得分:0)

使用ALAssetRepresentation ...

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    // Chooses the photo at the last index
    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets] - 1] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

        // The end of the enumeration is signaled by asset == nil.
        if (alAsset) {
            ALAssetRepresentation *representation = [alAsset defaultRepresentation];
            UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

或者如果你想要这条路......

            NSURL *imageUrl = [representation url];

答案 1 :(得分:0)

你有:

-(void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *imagem  = [info objectForKey: UIImagePickerControllerOriginalImage];
}

但是你说你不需要这个形象;你只需要路径。那将是:

-(void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSURL *imageurl  = [info objectForKey: UIImagePickerControllerReferenceURL];
}

现在你有了URL,你可以存储它;然后,如果需要,您可以在以后检索实际图像。

要从资产网址获取图片,请在此处查看我的回答:https://stackoverflow.com/a/22968941/341994