1

我想将我的应用生成的图像上传到 Dropbox。我从 DBRESTClient 看到了上传方法,但在我看来,我必须在调用上传方法之前将图像写入临时文件。

有没有办法从内存中的对象上传文件?像这样的东西:

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

2 回答 2

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文件然后删除它?为此,您可以使用NSDatawriteToFile:atomically:writeToFile:options:error :。

于 2011-03-23T17:16:59.307 回答
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];
}
于 2011-03-26T21:16:52.017 回答