【问题标题】:How to save path of image picked from UIImagePickerViewController to sqlite database and access it later ? ios如何将从 UIImagePickerViewController 中选择的图像路径保存到 sqlite 数据库并稍后访问它? IOS
【发布时间】:2013-01-17 11:58:18
【问题描述】:

我需要保存从服务器选择的图像的路径,然后访问该路径以通过数据库检索在用户单击时显示该图像。

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

但它不起作用

【问题讨论】:

    标签: ios uiimageview uiimagepickercontroller


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

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

      更新:

      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 在哪里是您的缓存网址

      【讨论】:

        猜你喜欢
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-02
        • 2021-04-08
        • 2021-02-07
        • 2020-11-30
        • 1970-01-01
        相关资源
        最近更新 更多