将内存中的 UIimage 上传到 iOS 中的 Dropbox

发布于 2024-10-25 23:58:29 字数 523 浏览 6 评论 0原文

我想将我的应用程序生成的图像上传到 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];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2024-11-01 23:58:29

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: of NSData 用于此目的。

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.

无人问我粥可暖 2024-11-01 23:58:29

这就是我实现解决方案的方式:

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文