gpt4 book ai didi

ios - Swift/XCode 与 Kingfisher - 无法从 Web 加载图像

转载 作者:行者123 更新时间:2023-11-30 13:27:51 30 4
gpt4 key购买 nike

我已经学习了有关 UIPageViewControllers 的教程,并且正在尝试加载我在 Amazon Web Services 中保存的图像。

我正在尝试滑动 UIPageController 上的不同项目,从 AWS 中提取各种图像,我收到:

"fatal error: unexpectedly found nil while unwrapping an Optional value.:

型号:

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
}
}

Controller :

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
}
}

enter image description here

最佳答案

正如之前的评论中提到的,您从 String 转换为 NSURL。“String”和“NSURL”是不同的对象类型。所以你需要进行一个转换。这会将您的字符串转换为 NSURL。

let imageUrl = NSURL(string: arrPagePhoto[index])

现在将此值与 Kingfisher 一起使用

pageContentViewController.imageView.kf_setImageWithURL(imageUrl)

关于ios - Swift/XCode 与 Kingfisher - 无法从 Web 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928652/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com