繁体   English   中英

如何从iOS应用程序在DropBox上上传图像

[英]How to Upload images on DropBox from iOS Application

我正在使用IOS应用程序,在该应用程序中,我从iPhone模拟器中选择照片,我想将这些照片上传到DropBox上,但我无法做到这一点。 我的上传方法中没有正确的参数。

这是我的代码,用于从控制器发送选定的图片。 此代码会将我从模拟器中选择的图像发送到控制器,在控制器中编写用于将图片上传到DropBox的代码:

// UIImage *image;
// NSMutableArray *images
// @property (nonatomic, copy) NSArray *chosenImages;

for (NSDictionary *dict in info)
{
    image = [dict objectForKey:UIImagePickerControllerOriginalImage];

    [images addObject:image];

    imageview = [[UIImageView alloc] initWithImage:image];

    [imageview setContentMode:UIViewContentModeScaleAspectFit];

    imageview.frame = workingFrame;

    self.chosenImages = images;

    RootViewController *rvc=[[RootViewController alloc]init];

    [rvc _startUpload:image];

    [self.navigationController pushViewController:rvc animated:YES];
}

现在在我的RootViewController中,我正在使用此方法编写用于上传图像的代码:_startUpload:image方法用于上传图片

-(void)_startUpload:image
{
    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];
}

我没有在Dropbox中获取照片,所以请帮助我,我做错了什么以及我真正用于上传图像的路径。 请帮我。

提前致谢。

有一个iOS SDK,可通过您的iOS应用程序提供API来与保管箱进行交互。以下是保管箱开发人员网站上提供的教程。

https://www.dropbox.com/developers/core/start/ios

以下是您需要查看的操作。

https://www.dropbox.com/developers/core/docs#files_put

在.h文件中:-

@interface UploadOptionViewController : UIViewController<UIImagePickerControllerDelegate,DBRestClientDelegate,UINavigationControllerDelegate>
{
 DBRestClient *restClient;
 NSString *file;
}
-(IBAction)photoLibrary:(id)sender;
@property (nonatomic,retain) NSString *file;
@property(retain,nonatomic) DBRestClient *restClient;
@end

在.m文件中:-

- (DBRestClient *)restClient {
if (!restClient) {
    restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    restClient.delegate = self;
}
return restClient;
}

 //-------------Upload file from dropbox --------------------------------------------

 - (void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath
          from:(NSString*)srcPath metadata:(DBMetadata*)metadata {
[presenter hideHud];
NSLog(@"File uploaded successfully to path: %@", metadata.path);
[[[[UIAlertView alloc]
   initWithTitle:@"File Uplaod" message:@"File uploaded successfully"
   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]
  autorelease]
 show];
 }

- (void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error {
[presenter hideHud];
NSLog(@"File upload failed with error - %@", error);
[[[[UIAlertView alloc]
   initWithTitle:@"File Upload" message:@"File upload failed"
   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]
  autorelease]
 show];
 }

并在适当的地方调用

NSString *localPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
NSString *filename = @"Info.plist";
NSString *destDir = @"/";
[[self restClient] uploadFile:filename toPath:destDir
               withParentRev:nil fromPath:localPath];

这里的filename是文件名,localpath是文件所在的位置。 希望这对您有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM