2011-03-23 60 views
1

我想將由我的應用生成的圖像上傳到Dropbox。我看到來自DBRestClient的上傳方法,但在我看來,我必須在調用上傳方法之前將該圖像寫入臨時文件。在iOS中將內存UIimage上傳到Dropbox

有沒有辦法從內存中的對象上傳文件?事情是這樣的:

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

回答

3

DBRestClient的頭中只透露

/* 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; 

iPhone有一個磁盤,所以與給定的方法上傳你的圖片爲tmp文件,然後刪除呢?爲此,您可以使用writeToFile:atomically:writeToFile:options:error:NSData

+0

您好尼克,感謝您的回答。我會嘗試這種方式。問候,Gazolla – Sebastian 2011-03-24 18:14:34

3

這就是我的方式實現的解決方案:

- (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]; 
} 
+0

謝謝@Sebastian,你能幫我把pdf從url上傳到Dropbox嗎? – 2014-09-01 08:36:18