我正在尝试使用Share扩展上传图像,但问题是当我添加后台任务时它给了我这个错误,它说后台任务不支持nsdata但是wwdc会话正在nsdata中上传图像.你能告诉我问题在哪里吗?以及我如何解决它
NSString *boundary = @"SportuondoFormBoundary"; NSString * configurationName = @"com.xxxxxxxx.PhotoSharing.backgroundConfiguration"; NSURLSessionConfiguration *configuration= [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationName]; [configuration setSharedContainerIdentifier:kGroupNameToShareData]; configuration.HTTPAdditionalHeaders = @{ @"api-key" : @"55e76dc4bbae25b066cb",@"Accept" : @"application/json",@"Content-Type" : [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] }; NSURLSession *session=[NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; NSMutableData *body = [NSMutableData data]; for (NSString *key in parameters) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n",[parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]]; } NSData *imageData = UIImageJPEGRepresentation(image,0.8); NSLog(@"imageDATE,%@",imageData); if (imageData) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n",@"file"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:imageData]; [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; } [body appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // Data uploading task. We could use NSURLSessionUploadTask instead of NSURLSessionDataTask if we needed to support uploads in the background NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kURLBase,kURLAddPostDL]]; NSLog(@"url %@",url); NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10000]; request.HTTPMethod = @"POST"; request.HTTPBody = body; NSURLSessionUploadTask *upload=[session uploadTaskWithRequest:request fromData:request.HTTPBody]; [upload resume];
解决方法
看来Up NSURLSessionUploadTask不支持大尺寸NSData,
但是你可以给NSURLSessionUploadTask一个文件路径上传到服务器上,或者把你的图像暂时写在磁盘上然后给出路径.
但是你可以给NSURLSessionUploadTask一个文件路径上传到服务器上,或者把你的图像暂时写在磁盘上然后给出路径.
Uploads using backgroundSessionConfiguration and NSURLSessionUploadTask cause app to crash
更新 :
//Create a file to upload UIImage *image = [UIImage imageNamed:@"onboarding-4@2x.png"]; NSData *imageData = UIImagePNGRepresentation(image); NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]; NSString *documentsDirectory = [[URLs objectAtIndex:0] absoluteString];
//这是filePath URL
NSString *filePath = [documentsDirectory stringByAppendingString:@"testfile.png"]; [imageData writeToFile:filePath atomically:YES]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://file.upload/destination"]]; [request setHTTPMethod:@"PUT"];
//这里使用的是filePath.
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath] completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) { //code }];