使用DropBox SDK for Iphone从UIImageView上传图像

我是iOS开发的初学者,刚刚开始使用iPhone的DropBox SDK,目前我正在使用Xcode 3.2.5(模拟器为4.2)的MacOSX 10.6版本。 使用UIImagePickerController,我可以在UIImageView上显示选定的图像。 现在,如果我想使用DropBox SDK上传特定的图像,我必须知道它在我的应用程序的path,如下面的代码应用

- (void)uploadFile:(NSString*)filename toPath:(NSString*)path fromPath:(NSString *)sourcePath

此方法在DBRestClient.h中定义,这是来自DropBox SDK for iOS的库文件。 但是从上面的方法声明中,需要确定UIImageView中存在的图像的“fromPath”,并将其上传到我的账户的Dropbox上。 你能帮我做一下如何确定path,或者就此而言,任何可以适用的工作。

您必须先将文件写入文件系统:

 NSData *data = UIImagePNGRepresentation(imageView.image); NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:@"upload.png"]; [data writeToFile:file atomically:YES]; [DBRestClient uploadFile:@"upload.png" toPath:@"Dropbox/Path" fromPath:file]; 

请注意,您也可以使用.jpg ,这是更快,更压缩,只需将UIImagePNGRepresentation更改为UIImageJPEGRepresentation ,并传递一个压缩值(0.8 – 0.9是好的)

为了从文档目录中读取文件(并与iTunes共享文档),请使用以下命令:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"FILE_NAME" stringByAppendingString:@".EXTENSION"]]; 

并在您的info.plist中设置关键Application supports iTunes file sharing为YES

为了读取embedded到您项目中的文件(不需要iTunes文件共享)并链接到文件系统中的文件夹,请使用以下命令:

 #define EXAMPLES_PATH @"/examplesPics/" - (NSString *) relativePathForExampleImage: (NSString *) fileName { return [[EXAMPLES_PATH stringByAppendingString:self.folderName] stringByAppendingString:fileName]; } - (NSString *) absolutePathForExampleImage: (NSString *) fileName { return [[[NSBundle mainBundle] bundlePath] stringByAppendingString:[self relativePathForExampleImage:fileName]]; } 

并将examplesPics文件夹添加到“Copy Bundle Resources”构build阶段。

如果你没有链接的文件夹,只需在你的项目中分组使用:

 - (NSString *) absolutePathForExampleImage: (NSString *) fileName { return [[[NSBundle mainBundle] bundlePath] stringByAppendingString:fileName]; }