我已经学习了UIPageViewControllers的教程,我正在尝试加载我在Amazon Web Services中保存的图像。
我正在尝试浏览我的UIPageController上的不同项目,从AWS中提取各种图像,我收到了:
“致命错误:在展开Optional值时意外发现nil。:
型号:
import UIKit
class PageContentViewController: UIViewController {
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var imageView: UIImageView!
var pageIndex: Int = 0
var strTitle: String!
var strPhotoName: String!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: strPhotoName)
lblTitle.text = strTitle
}
}
控制器:
import UIKit
import Kingfisher
class ViewController: UIPageViewController, UIPageViewControllerDataSource
{
var arrPageTitle: NSArray = NSArray()
var arrPagePhoto: NSArray = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
arrPageTitle = ["This is The App Guruz", "This is Table Tennis 3D", "This is Hide Secrets"];
arrPagePhoto = ["https://s3.amazonaws.com/fan-polls/heyward_again.jpg", "https://s3.amazonaws.com/fan-polls/Schwarber.jpg", "https://s3.amazonaws.com/fan-polls/mike_ditka.jpg"];
self.dataSource = self
self.setViewControllers([getViewControllerAtIndex(0)] as [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{
let pageContent: PageContentViewController = viewController as! PageContentViewController
var index = pageContent.pageIndex
if ((index == 0) || (index == NSNotFound))
{
return nil
}
index += 1;
return getViewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
{
let pageContent: PageContentViewController = viewController as! PageContentViewController
var index = pageContent.pageIndex
if (index == NSNotFound)
{
return nil;
}
index += 1;
if (index == arrPageTitle.count)
{
return nil;
}
return getViewControllerAtIndex(index)
}
func getViewControllerAtIndex(index: NSInteger) -> PageContentViewController
{
// Create a new view controller and pass suitable data.
let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
pageContentViewController.strTitle = "\(arrPageTitle[index])"
pageContentViewController.strPhotoName = "\(arrPagePhoto[index])"
pageContentViewController.imageView.kf_setImageWithURL(arrPagePhoto[index] as! NSURL)
pageContentViewController.pageIndex = index
return pageContentViewController
}
}
答案 0 :(得分:0)
如前面的评论所述,您将从String转换为NSURL。 '字符串'和' NSURL'是不同的对象类型。 所以你需要进行转换。这会将您的String转换为NSURL。
let imageUrl = NSURL(string: arrPagePhoto[index])
现在将此值与Kingfisher一起使用
pageContentViewController.imageView.kf_setImageWithURL(imageUrl)