我想从包含多个图像的 PFFile 中检索图像,并将它们放在一个滚动视图中。到目前为止,我已经尝试了以下方法,但这会返回错误“警告:正在主线程上执行长时间运行的操作。”
let scrView = UIScrollView()
var query = PFQuery(className: "Pictures")
query.whereKey("uploader", equalTo: PFUser.currentUser()!)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock{
(posts: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
for post in posts!{
self.imageFiles.append(post["imageFile"]as! PFFile)
self.imageText.append(post["imageText"]as! String)
}
for var i = 0; i < self.pictNumber; ++i {
var userImageFile = self.imageFiles[i]
println(userImageFile)
var imgFileData = userImageFile.getData()
var image = UIImage(data: imgFileData!)
var tempImageView = UIImageView (image: image)
tempImageView.frame = CGRectMake(CGFloat(width*i), 0, self.view.frame.width, self.view.frame.height)
scrView.addSubview(tempImageView)
}
我也尝试了以下方法,但它跳过了从 PFFile 中提取图像并将图像添加到滚动视图的部分。
let scrView = UIScrollView()
var query = PFQuery(className: "Pictures")
query.whereKey("uploader", equalTo: PFUser.currentUser()!)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock{
(posts: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
for post in posts!{
self.imageFiles.append(post["imageFile"]as! PFFile)
self.imageText.append(post["imageText"]as! String)
}
for var i = 0; i < self.pictNumber; ++i {
var userImageFile = self.imageFiles[i]
println(userImageFile)
//Extracting pictures from PFFile
self.imageFiles[i].getDataInBackgroundWithBlock{
(imageData: NSData?, error: NSError?) -> Void in
if imageData != nil {
var image = UIImage(data: imageData!)
var tempImageView = UIImageView (image: image)
tempImageView.frame = CGRectMake(CGFloat(width*i), 0, self.view.frame.width, self.view.frame.height)
scrView.addSubview(tempImageView)
println(i)
}else {
println(error)
}}
}