gpt4 book ai didi

IOS 使用 SDWebImage 从 URL 加载图像

转载 作者:行者123 更新时间:2023-11-28 12:42:14 26 4
gpt4 key购买 nike

我正在尝试使用以下内容从 json URL 加载图像,但在 [cell.imageView setImageWithURL:[NSURL URLWithString:@"coffeemugDirectLink"] placeholderImage:[UIImage imageNamed:@"heart .png"]]; 错误是 Expected ',' separatorExpected expression in container literal 我做错了什么?如何将 json 中的 URL 加载到我在 UICollectionView 中的单元格中?

import UIKit
import SDWebImage


class TopratedVC: BaseViewController {

@IBOutlet var collectionview: UICollectionView!
@IBOutlet var Image: UIImageView!

//Our web service url
let URL_GET_coffeemugs:String = "http://coffeemugs.com/ios/feed.php"


override func viewDidLoad() {
super.viewDidLoad()
addSlideMenuButton()
// Do any additional setup after loading the view.

//SDWebimage stuff
let imageView = UIImageView()


//created NSURL
let requestURL = NSURL(string: URL_GET_coffeemugs)


//creating NSMutableURLRequest
let request = NSMutableURLRequest(URL: requestURL!)

//setting the method to post
request.HTTPMethod = "GET"

//creating a task to send the post request
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in

//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}

//parsing the response
do {
guard let coffeemugs = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSArray else {
//Doesn't exist, or isn't an NSArray
return
}

for coffeemug in coffeemugs {
//getting the data at each index
let coffeemugName = coffeemug["name"] as! String
let coffeemugDirectLink = coffeemug["direct_link"] as! String
let coffeemugImage = coffeemug["image"] as! String

//displaying the data
print("name -> ", coffeemugName)
print("direct_link -> ", coffeemugDirectLink)
print("image -> ", coffeemugImage)
print("===================")
print()

}


} catch {
print(error)
}
}

// Make cell
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)


[cell.imageView setImageWithURL:[NSURL URLWithString:@"coffeemugDirectLink"] placeholderImage:[UIImage imageNamed:@"heart.png"]];

return cell
}


//executing the task
task.resume()

}

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

}

最佳答案

您的代码有几个问题。你问的那个是由于将一些 Objective-C 语法转储到 Swift 程序的中间造成的,但你更大的问题是你没有将从 API 检索到的数据存储在 cellForItemAtIndexPath可以得到它。

此外,cellForItemAtIndexPath 需要是 View Controller 类上的一个实例方法,它可以根据需要由 UICollectionView 调用。您不能将其作为内联函数并希望它会起作用。

您需要创建一个数组来存储您的杯子和一个结构体以放入该数组。

struct Mug {
var name: String
var directLink: String
var image: String
}

import UIKit
import SDWebImage

class TopratedVC: BaseViewController, UICollectionViewDataSource {

@IBOutlet var collectionview: UICollectionView!
@IBOutlet var Image: UIImageView!

var mugs = [Mug]()
var placeholderImage = UIImage(named:"heart.jpg")!

//Our web service url
let URL_GET_coffeemugs = "http://coffeemugs.com/ios/feed.php"

override func viewDidLoad() {
super.viewDidLoad()
// addSlideMenuButton()
// Do any additional setup after loading the view.

//created NSURL
if let requestURL = NSURL(string: URL_GET_coffeemugs) {
//creating NSMutableURLRequest
let request = NSMutableURLRequest(URL: requestURL)
//setting the method to post
request.HTTPMethod = "GET"
//creating a task to send the post request
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}

//parsing the response
do {
guard let coffeemugs = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSArray else {
//Doesn't exist, or isn't an NSArray
return
}

var newMugs=[Mug]()

for coffeemug in coffeemugs {
//getting the data at each index
let coffeemugName = coffeemug["name"] as! String
let coffeemugDirectLink = coffeemug["direct_link"] as! String
let coffeemugImage = coffeemug["image"] as! String

let newMug = Mug(name:coffeemugName,
directLink: coffeemugDirectLink,
image:coffeemugImage)
newMugs.append(newMug)
//displaying the data
print("name -> ", coffeemugName)
print("direct_link -> ", coffeemugDirectLink)
print("image -> ", coffeemugImage)
print("===================")
print()

}
dispatch_async(dispatch_get_main_queue(),{
self.mugs = newMugs
self.collectionview.reloadData()
})

} catch {
print(error)
}
}
//executing the task
task.resume()
}
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return self.mugs.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
let mug = self.mugs[indexPath.item]
if let imageURL = NSURL(string:mug.image) {
cell.imageView.setImageWithURL(imageURL,placeholderImage:self.placeholderImage)
} else {
cell.imageView.image = self.placeholderImage
}
return cell
}
}

一些纯粹主义者可能会提示我强行展开占位符图像,但老实说,如果这失败了,你的应用程序 Assets 就会出现问题,并且在开发时崩溃会告诉你这一点。

关于IOS 使用 SDWebImage 从 URL 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39154980/

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