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 = [[UIImag开发者_运维问答e 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];
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.
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];
}
精彩评论