[英]Upload an image NOT recompressed during transfer - iOS Objective c
目前,我可以使用其API和以下代碼將圖像發布到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的任何免費存儲服務,但我不希望以任何方式更改圖像。 這不是質量問題,但是我在上傳圖像之前對圖像進行了隱寫術,因此我真的不需要對其進行修改。 我只需要上傳它,並在上傳完成后接收它的鏈接。 有什么辦法嗎?
謝謝。
也許嘗試上傳圖像二進制數據而不是base64? 抱歉,我無法發表評論,所以我只是寫了這個作為答案。 這是鏈接 。
請確保更改名稱=(示例為fileupload
): [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\\"fileupload\\"; filename=\\"%@\\"\\r\\n", file]dataUsingEncoding:NSUTF8StringEncoding]];
從api相應地。
對於也為此感到掙扎的任何人。...使用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);
}
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.