共享扩展程序不上传完整大小的图像 [英] Share Extension not uploading full size images

查看:96
本文介绍了共享扩展程序不上传完整大小的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的iOS应用程序开发共享扩展。我真的做了所有的事情,但问题是我的代码只适用于小图片,但是当我上传从设备相机拍摄的图像时,上传失败,只有文字上传。

i am developeing share extension for my iOS application. i had really done every thing but the problem is that my code is only working for small images but when i upload image taken from device camer then uploading fails and only text get uploded.

- (void)performUploadWith:(NSDictionary *)parameters imageFile:(UIImage *)image{

NSString *boundary = @"SportuondoFormBoundary";


//    NSURLSessionConfiguration *configuration= [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationName];

//    NSURLSession *session=[NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
//    NSMutableURLRequest *request;//[NSURLRequest pho]

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
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];
}






我将我的代码更新为这和我检查服务器我的请求永远不会到达服务器。我正在使用此代码


i update my code to this and i check on server my request never reaches to server. I am using this code

        NSInteger randomNumber = arc4random() % 1000000;
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"testSession.foo.%d", randomNumber]];
    config.sharedContainerIdentifier=kGroupNameToShareData;
  session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
  NSURLSessionUploadTask  *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL        URLWithString:[NSString stringWithFormat:@"file://%@", dataSrcImagePath]]];

   [uploadTask resume];


推荐答案

问题是我没有使用后台会话

The problem is i am not using background session

  NSURLSessionConfiguration *configuration= [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:configurationName];







- (void)performUploadWith3:(NSDictionary *)parameters imageFile:(UIImage *)image

{

NSString *fileName = @"image.file";
NSString *boundary= @"Boundary+2EB36F87257DBBD4";

NSURL *storeURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:kGroupNameToShareData];


storeURL=[storeURL URLByAppendingPathComponent:fileName];
NSLog(@"sotre url %@",storeURL);

NSData *imageData =  UIImageJPEGRepresentation(image, 0.8);

imageData =[self appendParams:parameters andImage:image bondary:boundary];


if (!([imageData writeToFile:[storeURL path] atomically:YES]))
{
    NSLog(@"Failed to save file.");
}


NSString *string=@"URL here";


NSURL *url = [NSURL URLWithString:string];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];


NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

for (NSString *key in parameters)
{
    [request setValue:[parameters objectForKey:key] forHTTPHeaderField:key];

}


if (session == nil)
{
    //If this is the first upload in this batch, set up a new session

    //Each background session needs a unique ID, so get a random number
    NSInteger randomNumber = arc4random() % 1000000;


    sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"and unique name"]];
    sessionConfiguration.sharedContainerIdentifier=kGroupNameToShareData;
    // config.HTTPMaximumConnectionsPerHost = 1;


    session = [NSURLSession  sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

    //Set the session ID in the sending message structure so we can retrieve it from the
    //delegate methods later
    //            [sendingMessage setValue:session.configuration.identifier forKey:@"sessionId"];
    NSLog(@"config %@",sessionConfiguration.sharedContainerIdentifier);
}




if([[NSFileManager defaultManager]fileExistsAtPath:[storeURL path]])
{

    NSURLSessionUploadTask  *uploadTask = [session uploadTaskWithRequest:request fromFile:storeURL];
    [uploadTask resume];
}
else
    NSLog(@"file does not exsit");

    NSLog(@"desc %@",[session sessionDescription]);



    //   NSLog(@"state %ld", [uploadTask state]);

    }

这篇关于共享扩展程序不上传完整大小的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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