上传一张在传输过程中未重新压缩的图片 - iOS Objective c
Upload an image NOT recompressed during transfer - iOS Objective c
目前我可以使用他们的 API 和以下代码 post 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 编码(从我在类似的 posts 中看到的我们被迫这样做)。我想将 "myImage.jpg" 上传到任何具有 API 的免费存储服务,但我不希望以任何方式更改图像。这不是质量问题,但我在上传之前对图像进行了隐写术,所以我真的不需要修改它。我只需要上传它并在上传完成后接收它的 link。有什么办法吗?
谢谢。
也许尝试上传图像二进制数据而不是 base64?对不起,我不能发表评论,所以我只是写这个作为答案。这是 link.
请务必将名称=(示例为 fileupload
)从 api 相应地更改为 [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileupload\"; filename=\"%@\"\r\n", file]dataUsingEncoding:NSUTF8StringEncoding]];
。
对于同样为此苦苦挣扎的任何人......使用 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);
}
}];
目前我可以使用他们的 API 和以下代码 post 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 编码(从我在类似的 posts 中看到的我们被迫这样做)。我想将 "myImage.jpg" 上传到任何具有 API 的免费存储服务,但我不希望以任何方式更改图像。这不是质量问题,但我在上传之前对图像进行了隐写术,所以我真的不需要修改它。我只需要上传它并在上传完成后接收它的 link。有什么办法吗?
谢谢。
也许尝试上传图像二进制数据而不是 base64?对不起,我不能发表评论,所以我只是写这个作为答案。这是 link.
请务必将名称=(示例为 fileupload
)从 api 相应地更改为 [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"fileupload\"; filename=\"%@\"\r\n", file]dataUsingEncoding:NSUTF8StringEncoding]];
。
对于同样为此苦苦挣扎的任何人......使用 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);
}
}];