iOS将图像保存到数组时保持url数组顺序 [英] iOS Keep url array order when saving images into an array

查看:252
本文介绍了iOS将图像保存到数组时保持url数组顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作。

我有一个包含多个不同图像的URL的数组。这些图像将显示在滚动视图中,我希望保持此顺序,以便能够删除特定索引处的特定图像。

现在我正在使用for循环,首先我加载数​​组 NSNull 对象然后我运行循环用图像替换所有对象。这似乎并不真正有效,特别是当应用程序从后台加载时。

I'm trying to do the following.
I have an array that contains several URLs for different images. These images will be displayed into a scrollview and I want to keep this order to be able to delete specific images at a specific index.
Right now I'm using a for loop, first I load the array with NSNull objects and afterward I run the loop replacing all the objects with images. This doesn't seem to be really efficient, specially when the app loads from the background.

这是我的代码:

func downloadImagesfromUrlArray(){

    for var i=0;i < graphsURLlist.count; i++ {
        if let data = NSData(contentsOfURL: graphsURLlist[i]){
            let image = UIImage(data: data)
            pageImages.replaceObjectAtIndex(i, withObject: image!)
            //self.scaleImagesandScrollView()
        }else{
            NSLog("%@", i)
            var alert = myAlertController(title: nil, message: "Error trying to update the charts. Try again later \(i)",
                preferredStyle: UIAlertControllerStyle.Alert)


            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
        }
        //Enter queue for every image to download
    }

    dispatch_group_leave(self.group)
}

有没有更好的这样做的方法?
谢谢

Is there a better approach to do this? Thanks

推荐答案

您最好以异步方式下载图像并缓存它们。在下载图像之前,最好显示微调器,然后用图像替换它。

You better want to download images asynchronously and cache them. Before the images are downloaded it's better to display spinner, and then replace it with an image.

以下是我最新应用程序中的一些相关方法:

Here are a few related methods from my latest app:

func downloadAndCacheImg(url: String?, cell: AppCell, tableView: UITableView, indexPath: NSIndexPath) {
    if url == nil { return }

    let charactersToRemove = NSCharacterSet.alphanumericCharacterSet().invertedSet
    let cacheFilename = "".join(url!.componentsSeparatedByCharactersInSet(charactersToRemove))

    if let fullFilename = prefixFilenameWithDirectory(cacheFilename), img = UIImage(named: fullFilename) {
        cell.setAppImage(img)
        return
    }
    if let nsUrl = NSURL(string: url!) {
        NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: nsUrl), queue: NSOperationQueue.mainQueue(), completionHandler: { (response, data, error) -> Void in
            if let data = data, img = UIImage(data: data) {
                if let fullFilename = self.prefixFilenameWithDirectory(cacheFilename) {
                    data.writeToFile(fullFilename, atomically: true)
                }
                if let cell = tableView.cellForRowAtIndexPath(indexPath) as? AppCell {
                    cell.setAppImage(img)
                }
            }
        })
    }
}

func prefixFilenameWithDirectory(filename: String) -> String? {
    var dir = NSFileManager.defaultManager().URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true, error: nil)
    dir = dir?.URLByAppendingPathComponent(filename)
    return dir?.path
}

这篇关于iOS将图像保存到数组时保持url数组顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆