未分配UIImage(命名:字符串)

时间:2017-05-17 09:51:17

标签: ios swift swift3 uiimage

在Swift 3中,UIImageView(named: string_of_image)未分配。其他类似的问题是使用image.xcassets来存储图片,但我使用的是Assets.xcassets。也许它引起了一些错误,但我不认为这不是主要问题。这是我的代码:

contentViewController.swift

import UIKit
class ContentViewController: UIViewController {
    @IBOutlet weak var guideImage: UIImageView!

    var pageIndex:Int!
    var imageFile: String = "" {
        didSet {
            if let imageView = guideImage {
                imageView.image = UIImage(named: imageFile)
            }

        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print(imageFile)
        if let image = UIImage(named: imageFile) {
            guideImage.image = image

        }
        else {
            print("image not assigned")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

GuideViewController.swift

import UIKit

class GuideViewController: UIViewController, UIPageViewControllerDataSource {
    var PageViewController: UIPageViewController!
    var PageImage: Array<String>!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.PageImage = ["페이지1", "페이지2", "페이지3"]


        let pageController = self.storyboard!.instantiateViewController(withIdentifier: 일반사용자가이드컨트롤러) as! UIPageViewController
        pageController.dataSource = self

        if PageImage.count > 0 {
            let firstController = viewControllerAtIndex(index: 0)
            let startingViewControllers = [firstController]
            pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
        }

        self.PageViewController = pageController
        addChildViewController(self.PageViewController!)
        self.view.addSubview(self.PageViewController!.view)
        self.PageViewController!.didMove(toParentViewController:self)


        let appearance = UIPageControl.appearance()
        appearance.pageIndicatorTintColor = UIColor.gray
        appearance.currentPageIndicatorTintColor = UIColor.white
        appearance.backgroundColor = UIColor.darkGray


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    private func viewControllerAtIndex (index : Int) -> ContentViewController {

        let vc : ContentViewController = self.storyboard?.instantiateViewController(withIdentifier: 페이지컨텐츠뷰) as! ContentViewController

        vc.pageIndex = index
        vc.imageFile = self.PageImage[index] as String

        return vc
    }

    /**
     * 이전 ViewPageController 구성
     */
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        let vc = viewController as! ContentViewController
        var index = vc.pageIndex as Int

        if( index == 0 || index == NSNotFound) {
            return nil
        }

        index = index-1

        vc.guideImage.image = UIImage(named: self.PageImage[index])
        return self.viewControllerAtIndex(index: index)
    }


    /**
     * 이후 ViewPageController 구성
     */
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        let vc = viewController as! ContentViewController

        var index = vc.pageIndex as Int

        if( index == NSNotFound) {
            return nil
        }

        index += 1

        if (index == self.PageImage.count) {
            return nil
        }

        vc.guideImage.image = UIImage(named: self.PageImage[index])

        return self.viewControllerAtIndex(index:index)
    }


    /**
     * 인디케이터의 총 갯수
     */
    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return self.PageImage.count
    }


    /**
     * 인디케이터의 시작 포지션
     */
    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 0
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

如果您需要更多信息,请告诉我们。

(解决) 在UIImage(名为:StringOfFile),StringOfFile应该是英文...对于像这样遇到麻烦的人可能会有所帮助

1 个答案:

答案 0 :(得分:1)

添加您的文件的扩展名。 例如 [“페이지1.png”,“페이지2.jpg”,“페이지3.gif”]