使用alamofire

时间:2015-10-13 23:57:57

标签: ios swift alamofire

我有一个包含60个远程文件(图像)的列表,我想下载所有这些文件,但是当我尝试在这里下载15个图像时我的代码

    let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

    for var i = 1; i < 61; i++ {

        Alamofire.download(.GET, "https://domain.com/folder/\(i).jpg", destination: destination)
            .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                dispatch_async(dispatch_get_main_queue()) {
                    print("Total bytes read on main queue: \(totalBytesRead)")
                }
            }
            .response { _, _, _, error in
                if let error = error {
                    print("Failed with error: \(error)")
                } else {
                    print("Downloaded file successfully")
                }
        }

    }

任何想法???

1 个答案:

答案 0 :(得分:0)

func doDownload(i:int)
{
    if(i > 60)
    {
        return;
    }
    let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

    Alamofire.download(.GET, "https://domain.com/folder/\(i).jpg", destination: destination)
        .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
            dispatch_async(dispatch_get_main_queue()) {
                print("Total bytes read on main queue: \(totalBytesRead)")
            }
        }
        .response { _, _, _, error in
            if let error = error {
                print("Failed with error: \(error)")
            } else {
                print("Downloaded file successfully")
                doDownload(i + 1); //If you only want to download on success do it here
            }
            //Otherwise doDownload(i + 1)here;
    }
} 
相关问题