[英]Upload Image from UIImageView using DropBox SDK for Iphone
我是iOS開發的初學者,剛開始使用iphone的DropBox SDK,我目前正在使用MacOSX 10.6版本,上面裝有Xcode 3.2.5(模擬器為4.2)。 使用UIImagePickerController,我可以在UIImageView上顯示選定的圖像。 現在,如果我想使用DropBox SDK上傳該特定圖像,則必須知道其在我的應用程序中的路徑,因為應用了以下代碼
- (void)uploadFile:(NSString*)filename toPath:(NSString*)path fromPath:(NSString *)sourcePath
此方法在DBRestClient.h中定義,該文件是DropBox SDK for iOS的庫文件。 但是從上述方法聲明開始,需要確定UIImageView中存在的圖像的“ fromPath”,以將其上傳到我在Dropbox上的帳戶中。 能否請您幫助我確定路徑,或者就此而言,可以采取任何可行的措施。
您必須先將文件寫入文件系統:
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
,然后傳遞一個壓縮值( UIImageJPEGRepresentation
是好的)
要從“文檔目錄”中讀取文件(並與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文件夾添加到“復制捆綁資源”構建階段。
如果沒有鏈接文件夾,只需將其分組在項目中即可:
- (NSString *) absolutePathForExampleImage: (NSString *) fileName {
return [[[NSBundle mainBundle] bundlePath] stringByAppendingString:fileName];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.