Search code examples
iosobjective-cimgur

Upload an image NOT recompressed during transfer - iOS Objective c


Currently I am able to post an image to imGur using their API with the following code:

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];

The problem is that it encodes it in base64 prior to making the request (from what I could see in similar posts we are forced to). I want to upload "myImage.jpg" to whatever free storage service that has an API but I DON'T want the image to be altered in any way. This isn't a quality problem, but I am performing steganography on the image prior to uploading it so I really need it not to be modified. I just need to upload it and to receive its link when the upload is completed. Any way to do this ?

Thanks.


Solution

  • For anyone also struggling with this.... Using google's API Firebase did the trick.

     //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);
        }
    }];