从Parse加载特定图像到UIPageViewController:SWIFT

时间:2015-08-02 06:53:38

标签: swift parse-platform uipageviewcontroller

我正在使用测试方法来实现规则并且它正常工作。所以基本上我的代码是寻找图像,所有图像都是jpg类型,并命名为AnswerXImageY.jpg,其中X可以是任何整数,Y总是5或更小。然后我从数组中删除额外的对象,以便它只能显示在PageViewController上找到的图像数量。 这种方法运行正常。

import Foundation
import UIKit

class AnswerDetailsViewController : UIViewController, UIPageViewControllerDataSource {

    var pageViewController : UIPageViewController!

    var image1: String!
    var image2: String!
    var image3: String!
    var image4: String!
    var image5: String!

    var image1Int : Int!
    var scoreInt : Int!
    var scoreSilverInt: Int!

    var pageImages:NSMutableArray!

    override func viewDidLoad() {

        image1 = "Answer" + "\(image1Int)" + "Image1.jpg"
        image2 = "Answer" + "\(image1Int)" + "Image2.jpg"
        image3 = "Answer" + "\(image1Int)" + "Image3.jpg"
        image4 = "Answer" + "\(image1Int)" + "Image4.jpg"
        image5 = "Answer" + "\(image1Int)" + "Image5.jpg"

        self.pageImages = [image1,image2 ,image3,image4,image5 ]


        var path1 = NSBundle.mainBundle().pathForResource("Answer" + "\(image1Int)" + "Image1", ofType: "jpg")
        var path2 = NSBundle.mainBundle().pathForResource("Answer" + "\(image1Int)" + "Image2", ofType: "jpg")
        var path3 = NSBundle.mainBundle().pathForResource("Answer" + "\(image1Int)" + "Image3", ofType: "jpg")
        var path4 = NSBundle.mainBundle().pathForResource("Answer" + "\(image1Int)" + "Image4", ofType: "jpg")
        var path5 = NSBundle.mainBundle().pathForResource("Answer" + "\(image1Int)" + "Image5", ofType: "jpg")


        if (path1 == nil)
        {
            self.pageImages.removeObject(image1)
        }

        if (path2 == nil)
        {
            self.pageImages.removeObject(image2)
        }

        if (path3 == nil)
        {
            self.pageImages.removeObject(image3)
        }

        if (path4 == nil)
        {
            self.pageImages.removeObject(image4)
        }

        if (path5 == nil)
        {
            self.pageImages.removeObject(image5)
        }

        if (self.pageImages.count == 0)
        {
            self.pageImages = ["NoImageAvailable.jpg"]
        }

        self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController

        self.pageViewController.dataSource = self


        var startvc = self.viewControllerAtIndex(0) as ContentViewController

        var viewControllers = NSArray(object: startvc)

        self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)

        self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.height - 60)

        self.addChildViewController(self.pageViewController)
        self.view.addSubview(self.pageViewController.view)
        self.pageViewController.didMoveToParentViewController(self)

           }

我现在想要从Parse上传图片而不是使用我的资产。 我有以下代码不完整,只拉1个图像。请帮我弄清楚我应该如何查找特定图片,例如" Answer5ImageY"并将它们存储在数组中并在PageViewController中显示它们。非常感谢你提前。

import UIKit
import Parse
import Bolts

class UserSettingsViewController: UIViewController {


    @IBOutlet var animation2 : UIImageView!

    var competitionArray:NSMutableArray = []

    var fragenImages = [PFFile] ()


    override func viewDidLoad() {
        super.viewDidLoad()

        loadImages()

    }

    func loadImages()


    {

        var query = PFQuery(className: "EasyQuizImages")

        query.orderByAscending("AnswerImages")

        query.findObjectsInBackgroundWithBlock {
            (objects,error) -> Void in
            if error == nil {
                let imagesobjects = objects as! [PFObject]

                for object : PFObject in objects as! [PFObject] {
                    let image = object["AnswerImages"] as! PFFile

                    image.getDataInBackgroundWithBlock {
                        (imageData, error) -> Void in
                        if error == nil {

                        //let finalimage = UIImage(data: imageFile)

                        let finalimage = UIImage(data: imageData!)

                            self.animation2.image = finalimage
                            println("Success")


                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码不会只提取一个图像,它会拉动并下载所有图像。您只看到一个的原因是您始终将图像放入单个图像视图中,以便您看到下载的最后一个图像(并且可能会看到在下载之间图像转换时出现一些闪烁)。

您无法在解析时描述您的数据存储,但您应该考虑:

  1. 您有多少个图像对象,以及是否应该在查询中添加过滤器或获取所有对象
  2. 存储所有对象,然后在本地找到正确的对象以下载
  3. 文件
  4. 缓存您下载的文件,这样您就不需要再次下载
  5. 在任何情况下,您都应该拆分代码,以便查询对象并存储它们,然后获取图像文件并存储它们。这个商店可能应该放在一个带有答案和图像编号键的字典中,这样你就可以轻松地检查你是否有你想要的那个并获得它。