Ios7 使用NSURLSession在iPad后台上传照片

Ios7 使用NSURLSession在iPad后台上传照片,ios7,nsurlsessionuploadtask,Ios7,Nsurlsessionuploadtask,使用NSURLSessionPloadTask从资产库上载多张照片的最佳方法是什么 我现在正在使用NSURLSession后台上传。以下是我使用的代码: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; //getting path

使用NSURLSessionPloadTask从资产库上载多张照片的最佳方法是什么

我现在正在使用NSURLSession后台上传。以下是我使用的代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; //getting path of documents directory.
    NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:@"tempFile.dat"];      //appending file name to documents path.

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        Byte *buffer = (Byte*)malloc(rep.size);
        NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
        NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

        _totalBytesToSend += buffered;
        [_dataArray addObject:data];
    };

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        DLog(@"Can't get image - %@",[myerror localizedDescription]);
    };

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];

    //work needed here
    for (NSURL *url in _urlArray) {

        if(url != nil)
        {
            [assetslibrary assetForURL:url
                           resultBlock:resultblock
                          failureBlock:failureblock];
        }

    }

    dispatch_async(dispatch_get_main_queue(), ^(void){
        for (NSData *data in _dataArray) {

            if(![data writeToFile:documentsPath atomically:YES])
            {
                DLog(@"Saving image failed");
            }

            //Prepare upload request
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"my url here"]];
            [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
            [request setHTTPMethod:@"POST"];
            [request addValue:[[DTETokenManager sharedManager] loginToken] forHTTPHeaderField: @"X-CSRF-Token"];

            _uploadTask = [_session uploadTaskWithRequest:request fromFile:[NSURL URLWithString:[NSString stringWithFormat:@"file://%@", documentsPath]]];
            [_uploadTask resume];
        }
    });
这是一个好方法吗?我面临的一个问题是,我无法正确地更新进度条,其中我打算显示整个上传过程的进度。我正在使用以下代码:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{

    //Progress reportpo
    _totalByteSent += totalBytesSent;
    if(_totalBytesToSend && _totalByteSent)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:kBytesUploaded object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:(double)totalBytesSent/(double)totalBytesExpectedToSend] forKey:NOTIFICATION_PAYLOAD]];
    }

}
正如预期的那样,进度更新根本不正确:)


请指出一个更好的方法,因为我发现很难找到合适的教程来处理上传。不过,有很多原因可以解释后台下载。

因为我在这里没有得到任何响应,所以我最终将进度条全部转储,并添加了一个活动指示器,当所有下载完成时,该指示器停止旋转。

是否希望进度条显示已完成的上载数量,或者说每个任务的上传状态?我已经知道了如何跟踪每个文件的上传。正如我在回答中提到的,我现在只使用了一个活动指示器。当然,最好的方案是在上传时显示每个文件的进度