尝试使用NSURLSession上传图像时出错

时间:2015-09-30 13:44:49

标签: ios objective-c nsurlsession

我正在使用以下代码上传和映像:

- (IBAction)uploadPic:(UIButton *)sender {
    PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[self.imageReferenceURL] options:nil];
    PHAsset *asset = [result firstObject];
    if (asset) {
        PHImageManager *manager = [PHImageManager defaultManager];
        [manager requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
        // upload the `imageData`

            NSURL *fileURL = info[@"PHImageFileURLKey"];
            NSString *filename = [fileURL lastPathComponent];
            NSString *mimeType = [self mimeTypeForPath:filename];

            NSString *urlString = @"url";

            NSMutableURLRequest* request= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
            [request setHTTPMethod:@"POST"];
            NSString *boundary = @"---------------------------14737809831466499882746641449";
            NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
            [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
            NSMutableData *postbody = [NSMutableData data];
            [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
            [postbody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimeType] dataUsingEncoding:NSUTF8StringEncoding]];
            [postbody appendData:imageData];
            [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

            NSURLSessionTask *task = [[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:postbody completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Response  %@",responseString);
        }];
            [task resume];
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    self.imageReferenceURL = info[UIImagePickerControllerReferenceURL];

    [picker dismissViewControllerAnimated:YES completion:nil];
}

我收到了这个错误:

2015-09-30 10:35:42.921 udazz [874:453021] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* - [__ NSPlaceholderArray initWithObjects:count:]:尝试从对象[0]'插入nil对象 ***第一次抛出调用堆栈: (0x183804f5c 0x198307f80 0x1836ee30c 0x1836f9db4 0x100020240 0x188d923e4 0x188d92360 0x188d7ac88 0x188d91c78 0x188d918a8 0x188d8aadc 0x188d5ba2c 0x188d59f18 0x1837bc5a4 0x1837bc038 0x1837b9d38 0x1836e8dc0 0x18e83c088 0x188dc2f60 0x1000315a4 0x198b328b8) libc ++ abi.dylib:以NSException类型的未捕获异常终止

0 个答案:

没有答案