【问题标题】:Parse & Swift- Randomizing imagesParse & Swift - 随机化图像
【发布时间】:2015-07-21 20:59:09
【问题描述】:

首先,简单来说,如何将空白 UIImage 视图更改为我存储在 Parse 中的图像?这是我目前的代码

   var query = PFQuery(className:"Content")
    query.getObjectInBackgroundWithId("mlwVJLH7pa") {
        (post: PFObject?, error: NSError?) -> Void in
        if error == nil && post != nil {

            //content.image = UIImage

        } else {
            println(error)
        }
    }

除了替换空白的 UIImageView 之外,如何使替换为随机的图像?我假设我不能再使用 objectId,因为它特定于它所代表的行。

【问题讨论】:

  • 不知道为什么我一直这么说......

标签: ios xcode swift parse-platform


【解决方案1】:

我将首先使用 getObjectsInBackgroundWithBlock 从解析中检索 objectIds,然后在名为 objectId 的变量中从该数组中选择一个随机 objectId。这样一来,您就可以避免用户从 parse 中查询每个对象并使用大量数据来执行此操作。

其次我会

getObjectInBackgroundWithId(objectId)
    if error == nil {
        if let image: PFFile = objectRow["image"] as? PFFile{
        image.getDataInBackgroundWithBlock {
        (imageData: NSObject?, error: NSError?) Void in ->
           if let imageData = imageData {
             let imageView = UIImageView(image: UIImage(data: imageData))
           }
        }
     }

至少这对我有用。

【讨论】:

    【解决方案2】:

    首先,您需要使用query.findObjectsInBackgroundWithBlock 而不是query.getObjectInBackgroundWithId

    这会得到你所有的 PFObjects。从它返回的数组中抓取一个随机 PFObject,现在您有一个可以将 content.image 设置为的随机 PFObject。

    假设你的 PFObject 有一个 'image' 属性作为 PFFile(你应该使用它来存储 Parse 的图像。)

    通过执行类似于以下的操作,只需将 PFFile 转换为 UIImage:

    if let anImage = yourRandomPFObject["image"] as? PFFile {
         anImage.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
    
         let image = UIImage(data:imageData)
         content.image = image
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-03
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 2013-04-13
      相关资源
      最近更新 更多