【发布时间】:2013-11-30 13:10:14
【问题描述】:
我正在尝试将图像 from here 保存到设备的 Documents 目录,但我的方法不起作用。 不得不提的是,这种方法适用于图片like this:
我已经使用 iExplorer 检查了 Document 的目录,并且图像以正确的名称保存到正确的路径中,我还尝试手动下载图像并将其放入 Documents 目录,并且它工作正常。我下载的图片大小为900kbyte,但代码下载的图片大小为3.1mbyte。
我的代码是:
UIImage *staticResortMap = [self downloadImageWithStringURL:resort.staticPhotoPath];
staticPhotoPath = [_fileManager saveImage:staticResortMap usingName:kImageBaseNameResortStaticMap];
- (UIImage *)downloadImageWithStringURL:(NSString *)stringURL {
NSURL *photoURL = [NSURL URLWithString:stringURL];
NSData *data = [[NSData alloc] initWithContentsOfURL:photoURL];
return [[UIImage alloc] initWithData:data];
}
- (NSString *)saveImage:(UIImage *)image usingName:(NSString *)name {
NSString *directory = [_documentsDirectory stringByAppendingPathComponent:kDirectoryNameResortResources];
NSError * error = nil;
[_fileManager createDirectoryAtPath:directory
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (error != nil) {
NSLog(@"error creating directory: %@", error);
}
NSString *path = [directory stringByAppendingPathComponent:name];
[UIImageJPEGRepresentation(image, .9f) writeToFile:path atomically:YES];
return path;
}
我尝试过像这样使用保存的图像:
UIImage *image = [UIImage imageNamed:@"/var/mobile/Applications/0C62F621-C823-4DE2-B1CD-796EBAA459FB/Documents/resort_resources/static_map_image.jpg"];
UIImage *image1 = [UIImage imageNamed:@"/var/mobile/Applications/0C62F621-C823-4DE2-B1CD-796EBAA459FB/Documents/resort_resources/static_map_image.png"];
UIImage *image2 = [UIImage imageNamed:@"static_map_image.jpg"];
UIImage *image3 = [UIImage imageNamed:@"static_map_image.png"];
我的问题是,当我将图像保存到 Document 目录并尝试使用函数 [UIImage imagenamed:""] 进行分配时,函数返回 nil。但是,当我从互联网上的其他 URL 保存图像时,它正在工作。我知道的 pid 听起来很傻。
我做错了什么?
【问题讨论】:
标签: ios objective-c swift uiimageview uiimage