[英]How do I send array of urls to another view and convert to images?
首先,我需要解析JSON以获取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()
}
}
}
您现在所看到的是创建一个缓存对象,以相应地缓存下载的图像及其URL。 如果要将URL数组传递给另一个视图,则一种可能性是将缓存共享为全局实例(例如,单例对象)。 这样,从另一个角度来看,您只需检查高速缓存中是否存在该图像,然后从高速缓存中获取它,否则需要从服务器请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.