如何将从UIImagePickerViewController中选取的图像路径保存到sqlite数据库并稍后访问? IOS

时间:2013-01-17 11:58:18

标签: ios uiimageview uiimagepickercontroller

我需要保存从服务器中挑选的图像的路径,然后访问路径以通过数据库回溯在用户点击时显示该图像。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
      NSString *urlPath =  [info objectForKey:@"UIImagePickerControllerReferenceURL"]absoluteString];
}

并将其保存到数据库。

保存为 assets-library://asset/asset.JPG?id = E1136225-97DE-4BF4-A864-67E33D60737A& ext = JPG

然后我要导入Imageview

 UIImageView *iv = [[UIImageView alloc]init];
 iv.image = [UIimage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imagepath]]];

但它不起作用

2 个答案:

答案 0 :(得分:2)

试试这个:

  typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
    typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);    

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){

     ALAssetRepresentation *rep = [myasset defaultRepresentation];
     CGImageRef iref = [rep fullResolutionImage];

     if (iref){

            UIImage *myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
            [fileImage addObject:myImage];

             }      
    };      

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror){

         //failed to get image.
    };                          

    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
    [assetslibrary assetForURL:[filePath objectAtIndex:0] resultBlock:resultblock failureBlock:failureblock];

注意:确保您的[filePath objectAtIndex:0]NSUrl个对象。否则将其转换为NSUrl

示例:

ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];

NSURL myAssetUrl = [NSURL URLWithString:[filePath objectAtIndex:0]];

assetslibrary assetForURL:myAssetUrl resultBlock:resultblock failureBlock:failureblock];

答案 1 :(得分:1)

使用ALAssetsLibrary类的assetForURL:resultBlock:failureBlock:方法按URL检索图像。 有更多信息:http://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009722

UPD:

ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
[lib assetForURL: url resultBlock: ^(ALAsset *asset) {
    ALAssetRepresentation *r = [asset defaultRepresentation];
    self.imgView.image = [UIImage imageWithCGImage: r.fullResolutionImage];
}
    failureBlock: nil];

url在哪里是您的缓存网址