2013-03-07 72 views
-1

我有一個從服務器下載圖像的url
我將它作爲NSData存儲並將其存儲到本地路徑。現在
將圖像存儲在文檔目錄路徑中具有相同名稱和擴展名

thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[CNPlayerURL getUniqueIdAvailable],imageUrl]]]; 

[thedata writeToFile:localFilePath atomically:YES]; 

我的問題是我可以下載名和擴展名的,我在我的道路我存儲圖像。因爲現在我的localFilePath是有文件的目錄路徑與imagename追加(我給它一個.jpg擴展一個唯一的ID)

就像當我用我的網址在瀏覽器中

http:/myserverurl/groupid/199/blob 

它下載此圖片:

enter image description here

現在我想保存與該名稱擴展

+0

我不明白你的問題?你想在文件名中包含完整的url嗎?或者你能否重新提出你的問題?舉例說明你想達到什麼目的? – 2013-03-07 11:47:16

+0

從您的URL中取出'lastPathComponent',並將其附加到'DocumentDirectory'路徑,同時將其寫入本地。 – Ankur 2013-03-07 11:49:07

+0

你想要圖像名稱與分機。在你的圖片網址? – Mani 2013-03-07 11:49:20

回答

1

您可以使用方法NSURLResponse這樣獲得「可下載的文件名」。

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"Downloadable FileName: %@",[response suggestedFilename]); 
    NSLog(@"Downloadable MIMEType: %@",[response MIMEType]); 
    NSLog(@"Downloadable ContentLength: %lld",[response expectedContentLength]); 
    [webData setLength:0]; 
} 
+0

謝謝bhargavi..it作品 – KDeogharkar 2013-03-08 05:39:30

1
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *jpgFilePath = [NSString stringWithFormat:@"/%@/%@", YourDirectoryName,[imageUrl lastPathComponent]]; 
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:jpgFilePath]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath:getImagePath]) { 
     NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)]; 
     [data1 writeToFile:getImagePath atomically:YES]; 
    } 
+0

感謝您的響應,但它不工作 – KDeogharkar 2013-03-07 12:20:09

相關問題