简体   繁体   English

使用适用于iPhone的DropBox SDK从UIImageView上传图像

[英]Upload Image from UIImageView using DropBox SDK for Iphone

I am a beginner to iOS development and have just started working with the DropBox SDK for iphone, I am currently using the MacOSX 10.6 version having the Xcode 3.2.5 on it(the simulator is 4.2). 我是iOS开发的初学者,刚开始使用iphone的DropBox SDK,我目前正在使用MacOSX 10.6版本,上面装有Xcode 3.2.5(模拟器为4.2)。 Using UIImagePickerController, I could display a selected image on the UIImageView. 使用UIImagePickerController,我可以在UIImageView上显示选定的图像。 Now if I want to upload that particular image using DropBox SDK, I have to know its path on my application, as the following code is applied 现在,如果我想使用DropBox SDK上传该特定图像,则必须知道其在我的应用程序中的路径,因为应用了以下代码

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

this method is defined in DBRestClient.h, a library file from the DropBox SDK for iOS. 此方法在DBRestClient.h中定义,该文件是DropBox SDK for iOS的库文件。 But as from the above declaration of the method, the "fromPath" of the image which is present in the UIImageView needs to be ascertained to upload it to my account on dropbox. 但是从上述方法声明开始,需要确定UIImageView中存在的图像的“ fromPath”,以将其上传到我在Dropbox上的帐户中。 Can you please help me in how to determine the path, or for that matter, any work around which can be applicable. 能否请您帮助我确定路径,或者就此而言,可以采取任何可行的措施。

You would have to write the file to the file system first: 您必须先将文件写入文件系统:

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

Note that you could use .jpg too, which is faster, and more compressed, just change UIImagePNGRepresentation to UIImageJPEGRepresentation , and pass a compression value (0.8 - 0.9 is good) 请注意,您也可以使用.jpg ,它速度更快,压缩率更高,只需将UIImagePNGRepresentation更改为UIImageJPEGRepresentation ,然后传递一个压缩值( UIImageJPEGRepresentation是好的)

For reading files from Documents Directory (and share documents with iTunes) use this: 要从“文档目录”中读取文件(并与iTunes共享文件),请使用以下方法:

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

And also set in your info.plist the key Application supports iTunes file sharing to YES 并在您的info.plist中设置关键Application supports iTunes file sharing为YES

For read a file embedded in your project (no iTunes file sharing) and linked with a folder from your File System use this: 要读取项目中嵌入的文件(不共享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]];
}

and also add the examplesPics folder to the "Copy Bundle Resources" Build Phase. 并将examplesPics文件夹添加到“复制捆绑资源”构建阶段。

If you don't have a linkded folder, just grouped in your project use this: 如果没有链接文件夹,只需将其分组在项目中即可:

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

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

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