【问题标题】:Save file path to realm.io and assign image with AlamofireImage将文件路径保存到 realm.io 并使用 AlamofireImage 分配图像
【发布时间】:2015-10-15 11:39:01
【问题描述】:

我有一个视图控制器,它拍摄一张照片,然后转到第二个视图控制器来预览捕获的图像。 那么问题来了,如何使用 alamofireimage 将我的图像分配给我的 imageview? 这是我收到的路径

assets-library://asset/asset.JPG?id=16F4079E-38B2-4B7C-953A-22395A528B5D&ext=JPG

谁能建议我如何使用它?

我的代码如下

let imageData = UIImageJPEGRepresentation(self.image!, 0.8)
    let compressedImage = UIImage(data: imageData!)
    ALAssetsLibrary().writeImageToSavedPhotosAlbum(compressedImage?.CGImage, orientation: ALAssetOrientation(rawValue: compressedImage!.imageOrientation.rawValue)!, completionBlock: {
        (path:NSURL!, error:NSError!) -> Void in
        print("\(path)")

        self.previewImageView.af_setImageWithURL(path)

    })

预览控制器不在模拟中。但是snapping后要segue,确认后返回collectionview控制器

【问题讨论】:

    标签: ios swift2 alamofire


    【解决方案1】:

    AlamofireImage 其方法setImageWithURL 用于从服务器异步加载图像。但是你有资产 URL,哪个图像已经存储在你的设备中,所以不要使用上面的 AlamofireImage 方法。

    使用以下代码从 ALAsset URL 加载图像:

    let imageData = UIImageJPEGRepresentation(self.image!, 0.8)
        let compressedImage = UIImage(data: imageData!)
        ALAssetsLibrary().writeImageToSavedPhotosAlbum(compressedImage?.CGImage, orientation: ALAssetOrientation(rawValue: compressedImage!.imageOrientation.rawValue)!, completionBlock: {
            (path:NSURL!, error:NSError!) -> Void in
            print("\(path)")
    
              assetLibrary.assetForURL(path, resultBlock: { (asset: ALAsset!) -> Void in  
                            if asset != nil {
                                 var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
                                 var iref = assetRep.fullScreenImage().takeUnretainedValue()
                                 var image =  UIImage(CGImage: iref)
                                 self.previewImageView.image = image     
                             }
    
                          }, failureBlock: { (error: NSError!) -> Void in
                                    println(error.localizedDescription)
                       })
    
        })
    

    【讨论】:

    • 如果为了一致性我更喜欢使用 AlamofireImage 怎么办?因为在我的集合视图控制器中,我使用 AlamofireImage 将图像加载到集合项目中
    • 您不想在这种特殊情况下使用 AlamofireImage 的 UIImageView 扩展。相反,请遵循@Kirit 的建议,直接使用 UIImageView 。 AlamofireImage UIImageView 扩展旨在与远程图像交互,而不是与本地图像交互。 AlamofireImage 的所有其他部分仍然很有用,但我会避免以这种方式使用 UIImageView 扩展。
    猜你喜欢
    • 2015-10-10
    • 2017-02-11
    • 2020-06-23
    • 1970-01-01
    • 2012-01-18
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多