gpt4 book ai didi

ios - iPhone:下载图像然后在带有@2x 扩展名的 UIImageView 中显示

转载 作者:可可西里 更新时间:2023-11-01 05:41:32 24 4
gpt4 key购买 nike

我正在尝试从 NSUrl imageURL 下载图像,并通过添加 @2x 将其显示在具有视网膜像素密度的 UIImageView 中;这将显示在 iPhone 4 或更高版本上。我使用了这里的一些代码:How do I download and save a file locally on iOS using objective C?

当我启动它时,它在 imageView 上没有显示任何内容,最后一行 NSLog 打印出图像的一些非常小的尺寸,表明它并没有真正下载图像。除了错误检查非常糟糕之外,我的代码还有什么问题?

- (void) presentImage: (NSURL*) imageURL{
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
NSLog([imageURL description]);

if ( imageData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"0@2x.png"];
[imageData writeToFile:filePath atomically:YES];
}
else NSLog(@"Error when loading image!!!");

imageData = [NSData dataWithContentsOfFile:@"0@2x.png"];
UIImage *image = [[UIImage alloc] initWithData: imageData];
NSLog(@"%f1 x %f2", image.size.width, image.size.height);
[self.imageView setImage: image];

编辑:好的,我修好了。我觉得我好笨。我应该把

imageData = [NSData dataWithContentsOfFile:@filePath];

并将 filePath 放入范围内,而不是仅仅放入 @"0@2x.png"。

最佳答案

我认为你把问题复杂化了。使用此方法:

+ (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale

当您有视网膜图像时,将比例设置为 2.0

-(void)presentImage:(NSURL*)imageURL {
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData scale:2.0];
[self.imageView setImage: image];
}

关于ios - iPhone:下载图像然后在带有@2x 扩展名的 UIImageView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16841926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com