Sebastian
Sebastian

Reputation: 6414

Upload an in memory UIimage to Dropbox in iOS

I want to upload an image generated by my app to Dropbox. I see the upload method from DBRestClient, but it seems to me that I have to write the Image to a temp file before calling the upload method.

Is there any way to upload file from an object in memory? Something like this:

UIimage *myImage = [[UIImage alloc]....
//
// Here I create the image on my app
//
NSData *myData = UIImageJPEGRepresentation(myImage, 1.0);
DBRestClient *rc = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
self.restClient = rc;
[rc release];
self.restClient.delegate = self;
[self.restClient uploadFile:@"myImage.jpg" toPath:@"DropBoxPath" fromPath:myData];

Upvotes: 1

Views: 1949

Answers (2)

Sebastian
Sebastian

Reputation: 6414

That's the way I implemented the solution:

- (void) uploadToDropBox {
    self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    self.restClient.delegate = self;
    [self.restClient createFolder:dropBoxFolder];

    NSString *fileName = @"myImage.png"; 
    NSString *tempDir = NSTemporaryDirectory();
    NSString *imagePath = [tempDir stringByAppendingPathComponent:fileName];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(self.loadQRCodeImage.image)];
    [imageData writeToFile:imagePath atomically:YES];

    [self.restClient uploadFile:fileName toPath:dropBoxFolder fromPath:imagePath];
}

Upvotes: 3

Nick Weaver
Nick Weaver

Reputation: 47241

The header of DBRestClient does only reveal

/* Uploads a file that will be named filename to the given root/path on the server. It will upload
   the contents of the file at sourcePath */
- (void)uploadFile:(NSString*)filename toPath:(NSString*)path fromPath:(NSString *)sourcePath;

The iPhone has a disk, so upload your image as tmp file with the given method and delete it afterwards? You can use writeToFile:atomically: or writeToFile:options:error: of NSData for that purpose.

Upvotes: 3

Related Questions