-1

我有一个 url,它从服务器下载图像并将
NSData存储到我的本地路径。

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

[thedata writeToFile:localFilePath atomically:YES];

现在我的问题是我是否可以下载存储在路径中的图像的名称和扩展名。因为现在我localFilePath的文档目录路径附加了图像名(我给它一个带有.jpg扩展名的唯一 ID)

就像我在浏览器中使用我的 url

http:/myserverurl/groupid/199/blob

它正在下载此图像:

在此处输入图像描述

现在我想用这个名称和扩展名保存

4

2 回答 2

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];
    }
于 2013-03-07T11:54:38.660 回答
1

suggestedFilename您可以使用这样的方法获取“可下载文件名” 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];
}
于 2013-03-08T05:33:51.963 回答