gpt4 book ai didi

swift - 使用 Swift 从 Api 下载图像并在 CollectionViewController 中填充图像

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

我能够在数组中找到响应数据,但无法在 Collection View 中下载和填充。我尝试从应用程序的图像容器上传图像,但无法通过 API 下载和上传

代码

    func get_data_from_url(){


//API calls
let url = NSURL(string: "http://android.eposapi.co.uk/?app_id=A1A2A3A4&app_key=K1K2K3K4&request=gallery")



let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
let postString = "gallery"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data,response,error in

if error != nil{
print("error=\(error)")
return
}
//Print out response object
print("response= \(response)")
//print response body
// let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)

// print("response data = \(responseString!)")

var json: NSArray!
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as? NSArray
print(json)
print(json[0])

} catch {
print(error)
}

dispatch_async(dispatch_get_main_queue()) {
self.collectionView!.reloadData()
}


}
task.resume()



override func viewDidLoad() {
super.viewDidLoad()
get_data_from_url()


self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)


}



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

// Configure the cell
let image = cell.viewWithTag(1) as! UIImageView

image.image = images[indexPath.row]



return cell
}

告诉我任何一种解决方案如何使用mutable urlRequest下载任何类型(png或jpg)的图像并填充到UICollectionView中/

最佳答案

我最近就这么做了,这是一个内存游戏,可以从 Instagram 中获取图像并将图像显示在 UICollectionView 中。请查看SwiftIntro project on Github

我使用 Alamofire 获取图像:

func prefetchImages(urls: [URLRequestConvertible], done: Closure) {

imageDownloader.downloadImages(URLRequests: urls) {
response in
done()
}
}

这是一个“预取”解决方案,然后我可以使用此函数检索图像:

func imageFromCache(url: NSURL) -> UIImage? {
guard let cache = imageCache else { return nil }
let imageFromCache = cache.imageForRequest(NSURLRequest(URL: url), withAdditionalIdentifier: nil)
return imageFromCache
}

查看 ImagePrefetcher class .

MemoryDataSourceClass实现 UICollectionViewDataSource 和 UICollectionViewDelegate 协议(protocol)返回 CardCVCell 类型的 UICollectionViewCells其中包含在其 .Xib 中创建的 UIImageView。我在这个方法中设置了 UIImageView 上的图像:

func updateWithModel(model: Model) {
guard let card = model as? CardModel else { return }
guard let cachedImage = ImagePrefetcher.sharedInstance.imageFromCache(card.imageUrl) else { return }
cardFrontImageView.image = cachedImage
flipped = card.flipped
}

对 ImagePrefetcher.sharedInstance 单例感到抱歉;),单例很糟糕(如 herehere 所讨论的)!我还没有使用惊人的 Swinject 设置依赖注入(inject),但很快就会这样做! :)

关于swift - 使用 Swift 从 Api 下载图像并在 CollectionViewController 中填充图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37616494/

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