首先,我需要解析JSON以获取URL。使用那些url我将它们转换为图像以加载UICollectionView。我需要发送一组URL来为其他视图执行相同的操作。问题是我需要将初始图像设置为我刚刚单击的缩略图,然后通过数组循环显示以下图像。这是我的代码,请帮忙!
class HomepageCollectionViewController: UICollectionViewController {
var imageCache = NSCache()
var hingeImagesArray = [HingeImage]()
var arrayToHoldConvertedUrlToUIImages = [UIImage]()
var task: NSURLSessionDataTask?
//full of urls
var hingeImageUrls = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Makes the network call for HingeImages
refreshItems()
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return hingeImagesArray.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageReuseCell", forIndexPath: indexPath) as! ImageCollectionViewCell
let image = hingeImagesArray[indexPath.row]
if let imageURL = image.imageUrl {
if let url = NSURL(string: imageURL) {
//Check for cached images and if found set them to cells - works if images go off screen
if let myImage = imageCache.objectForKey(image.imageUrl!) as? UIImage {
cell.collectionViewImage.image = myImage
}else {
// Request images asynchronously so the collection view does not slow down/lag
self.task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
// if (response as! NSHTTPURLResponse == 400) {
// print("400")
// }
// Check if there is data returned
guard let data = data else {
return
}
// Create an image object from our data and assign it to cell
if let hingeImage = UIImage(data: data){
//Cache images/set key for it
self.imageCache.setObject(hingeImage, forKey: image.imageUrl!)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cell.collectionViewImage.image = hingeImage
})
}
})
task?.resume()
}
}
}
return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
performSegueWithIdentifier("nextView", sender: cell)
}else{
print("no good")
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//Load imageView with the image clicked on
//Must send over whole array with images to display in next screen
if let indexPath = self.collectionView?.indexPathForCell(sender as! ImageCollectionViewCell) {
if segue.identifier == "nextView" {
let galleryViewController = segue.destinationViewController as! GalleryViewController
//We are passing the url to the next screen then making another request to show them
let imageUrl = hingeImagesArray[indexPath.row]
//passesd a single image
galleryViewController.imageUrlFromOtherScreen = imageUrl.imageUrl
//Pasing array of urls
galleryViewController.arrayOfImageUrlsFromOtherView = hingeImageUrls
//let image = arrayToHoldConvertedUrlToUIImages[indexPath.row]
//galleryViewController.selectedImageFromPreviousScreen = image
// galleryViewController.arrayOfImageObjects = arrayToHoldConvertedUrlToUIImages
//print(arrayToHoldConvertedUrlToUIImages)
}
}
}
//Execute JSON request
func refreshItems() {
HingeClient.executeJSONNetworkRequest { (hingeImages, error) in
guard let images = hingeImages else {
print("Error getting images with JSON")
return
}
//Loop through array and append URLs to new array
for moreImages in hingeImages! {
self.hingeImageUrls.append(moreImages.imageUrl!)
}
self.hingeImagesArray = images
self.collectionView?.reloadData()
}
}
}
答案 0 :(得分:0)
我现在所看到的是创建一个缓存对象来相应地缓存下载的图像及其URL。如果要将URL数组传递给另一个视图,则可以将缓存作为全局实例(例如单个对象)共享。因此,从另一个视图来看,您只需检查缓存是否存在图像,然后从缓存中获取,否则需要从服务器请求。