下载并保存一个PFFile(UIImage)以显示在UIImageView:SWIFT中

时间:2015-05-04 10:24:10

标签: swift parse-platform uiimageview uiimage pffile

我在解析时有一个叫做“目标”的大班。这个类有多个元素,其中一个是PFFile,它总是一个UIImage。

当我对“目标”类执行查询时,我无法弄清楚如何使用PFFile,并将其更改为UIImage以供使用。

         var query = PFQuery(className:"Goal")

    let currentUser = PFUser.currentUser()!.username
    query.whereKey("creator", equalTo: currentUser!)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects?.count) goals for the TableView.")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                for object in objects {

                    let goalType = object["type"] as! String
                    let goalPeriod = object["period"] as! String
                    let goalCategory = object["category"] as! String
                    let goalShortDescription = object["shortDescription"] as! String
                    let goalLongDescription = object["longDescription"] as! String
                    let goalPointvalue = object["pointValue"] as! Int
                    let goalSharedSetting = object["shared"] as! Bool
                    let goalAdoptionCount = object["adoptionCount"] as! Int
                    let goalIsComplete = object["isComplete"] as! Bool

                    let goalSuccessImageData = object["image"] as! PFFile

                    goalSuccessImageData.getDataInBackgroundWithBlock {
                        (imageData: NSData?, error: NSError?) -> Void in
                        if error == nil {
                            if let imageData = imageData {
                                let image = UIImage(data:imageData)
                                self.imageQuery = image
                            }
                        }
                    }
                    let goalSuccessImage : UIImage = self.imageQuery

                    let goalObjectID = object.objectId
                    let goalSpreadCount = object["spreadCount"] as! Int
                    let goalSpreadTotal = object["spreadTotal"] as! Int
                    let goalTotalCompletions = object["totalCompletions"] as! Int


                    let thisGoal = GoalModel(period: goalPeriod, type: goalType, category: goalCategory, shortDescription: goalShortDescription, longDescription: goalLongDescription, pointValue: goalPointvalue, shared: goalSharedSetting, adoptionCount: goalAdoptionCount, isComplete: goalIsComplete, successImage: goalSuccessImage, goalID: goalObjectID!, spreadCount: goalSpreadCount, spreadTotal: goalSpreadTotal, totalCompletions: goalTotalCompletions ) as GoalModel

有关如何修改“成功图片”部分的任何提示?我之前和之后都添加了一个空格,以便于查找。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

我在我的项目中使用这种方式,如果它对你有用:

func performSave(sender: UIBarButtonItem){
    affichageActivityIndicator()
    let qos = Int(QOS_CLASS_USER_INITIATED.value)
    dispatch_async(dispatch_get_global_queue(qos,0)) { () -> Void in
        dispatch_async(dispatch_get_main_queue()){
            if let updateObject = self.currentObject as PFObject? {
                let imageData = UIImageJPEGRepresentation(imageToSave, 0.1)
                let imageFile = PFFile(name:"image.png", data:imageData)

                updateObject["imageFile"] = imageFile

                // Save the data back to the server in a background task
                updateObject.saveInBackgroundWithBlock{(success: Bool, error: NSError!) -> Void in
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    if success == false {
                        println("Error")
                    }
                }
            }
        }
    }
}