上传未在传输过程中重新压缩的图像 - iOS目标c

时间:2017-11-09 15:49:14

标签: ios objective-c imgur

目前,我可以使用以下代码将图像发布到imGur:

NSString *clientID = @"myID";
NSString *XMashapeKey = @"mySecretID";

NSMutableCharacterSet *allowedCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet];
        [allowedCharacterSet addCharactersInString:@"-._~"];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"myImage.jpg"], 1.0);
NSString *base64Img = [imageData base64EncodedStringWithOptions:0];
NSString *escapedBase64 = [base64Img stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.imgur.com/3/upload"]];
    [request setHTTPMethod:@"POST"];

NSString *postString = [NSString stringWithFormat:@"image=%@", escapedBase64];

[request setValue:XMashapeKey forHTTPHeaderField:@"X-Mashape-Key"];
[request setValue:[NSString stringWithFormat:@"Client-ID %@", clientID] forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody: [postString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (error) {
        NSLog(@"%@", error);
    }
    else {
        NSLog(@"%@", response);
        id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"%@", obj);
    }
}];
[task resume];

问题是它在发出请求之前在base64中对其进行编码(从我在强制要求的类似帖子中可以看到)。我想将“myImage.jpg”上传到任何具有API的免费存储服务,但我不希望以任何方式更改图像。这不是质量问题,但我在上传之前对图像执行隐写术,所以我真的需要它不被修改。我只需要上传它并在上传完成时接收其链接。有什么办法吗?

感谢。

2 个答案:

答案 0 :(得分:0)

也许尝试上传图片二进制数据而不是base64?对不起,我无法评论,所以我只是写了这个作为答案。这是link

请务必相应地从api更改名称=(示例为fileupload):[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileupload\"; filename=\"%@\"\r\n", file]dataUsingEncoding:NSUTF8StringEncoding]];

答案 1 :(得分:0)

对于任何也在努力解决这个问题的人....使用google的API Firebase可以解决问题。

 //Points to the root reference
FIRStorageReference *storageRef = [[FIRStorage storage] reference];
// File located on disk
NSURL *localFile = [NSURL fileURLWithPath:@"your/local/path"];

// Create a reference to the file you want to upload
FIRStorageReference *riversRef = [storageRef child:@"your/path/in/firebase/server/yourimage.jpg"];

// Upload the file to
FIRStorageUploadTask *uploadTask = [riversRef putFile:localFile metadata:nil completion:^(FIRStorageMetadata *metadata, NSError *error) {
    if (error != nil) {
        // Uh-oh, an error occurred!
    } else {
        // Metadata contains file metadata such as size, content-type, and download URL.
        NSURL *downloadURL = metadata.downloadURL;
        // NSLog(@"%@", downloadURL);
    }
}];