2012-01-20 46 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]; 
}