2012-01-20 47 views
0

我是iOS开发的初学者,刚刚开始使用iPhone的DropBox SDK,我目前使用的是MacOSX 10.6版本,它具有Xcode 3.2.5(模拟器是4.2)。使用UIImagePickerController,我可以在UIImageView上显示选定的图像。现在,如果我想用的Dropbox SDK来上传特定图像,我知道我的应用它的路径,如下面的代码应用使用DropBox SDK for iPhone上传UIImageView中的图像

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

这种方法在DBRestClient.h,一个库文件中定义来自iOS的DropBox SDK。但是从上面的方法声明中,需要确定存在于UIImageView中的图像的“fromPath”,以将其上传到我的Dropbox上的帐户。您能否帮助我确定路径,或者就此而言,可以适用哪些工作。

回答

11

你将不得不将文件写入到文件系统第一:

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过,这是更快,更多的压缩,只是改变UIImagePNGRepresentationUIImageJPEGRepresentation,并通过压缩值(0.8 - 0.9是好的)

+0

非常感谢!它的工作完美! :) – An1Ba7

+1

它应该是...'fromPath:file' – Mundi

+0

@AnkurBarthakur如果我的解决方案工作,为什么你不接受它? –

0

对于从文件目录(和共享文档与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

对于阅读嵌入项目(没有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”构建阶段。

如果没有linkded文件夹,只需在您的项目组合使用:

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