如何保存图像的路径从UIImagePickerViewController挑SQLite数据库,并

2019-08-07 04:23发布

我需要保存从服务器选择了图像的路径,并在稍后访问的路径通过数据库retrival显示在用户点击该图像。

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

并将其保存到数据库中。

它保存为资产库://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]]];

但它不工作

Answer 1:

使用assetForURL:resultBlock:failureBlock: ALAssetsLibrary类的方法,通过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被缓存的URL



Answer 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];


文章来源: How to save path of image picked from UIImagePickerViewController to sqlite database and access it later ? ios