Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
视网膜与非视网膜iOS_Ios_Image_Uiimageview_Retina Display - Fatal编程技术网

视网膜与非视网膜iOS

视网膜与非视网膜iOS,ios,image,uiimageview,retina-display,Ios,Image,Uiimageview,Retina Display,我有一些关于iOS中视网膜和非视网膜图像的问题。实际上,我正在下载一些图像文件,它们没有附加@2x后缀。我有几个问题 1-首先,它不在捆绑包中,下载后只在文档库中,因此@2x不会像捆绑的视网膜图像那样工作。我的假设正确吗 2-视网膜的大小是非视网膜图像的两倍,但如果你看到视网膜图像缩放为2.0,那么如果我手动将任何图像缩放为2.0,会有任何质量差异吗?e、 g.我有一个图像Image1.png,并将其转换为缩放2.0,只需添加到UIImageView中,而在另一侧,我有相同的图像,但名称相同Im

我有一些关于iOS中视网膜和非视网膜图像的问题。实际上,我正在下载一些图像文件,它们没有附加@2x后缀。我有几个问题

1-首先,它不在捆绑包中,下载后只在文档库中,因此@2x不会像捆绑的视网膜图像那样工作。我的假设正确吗

2-视网膜的大小是非视网膜图像的两倍,但如果你看到视网膜图像缩放为2.0,那么如果我手动将任何图像缩放为2.0,会有任何质量差异吗?e、 g.我有一个图像Image1.png,并将其转换为缩放2.0,只需添加到UIImageView中,而在另一侧,我有相同的图像,但名称相同Image2@2x.png我在UIImageView中添加了Image2。与Image2相比,Image1中是否存在任何质量差异

这里有一个代码片段,我用它将图像转换成2.0或视网膜,如果图像是非视网膜的

UIImage *image = [UIImage imageNamed:@"fileName"];

UIImage *convertedImage = [UIImage imageWithData:UIImagePNGRepresentation(image) scale:2.];

下载的图像放置在文档库中。无法使用
[UIImage ImageName:@“filename”]
获取图像,因为这些函数仅从捆绑包获取图像。下面是如何从文档库获取图像的示例:

NSString *bundlePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath, fileName]];

缩放非视网膜图像以获得视网膜大小是不好的做法。我的建议是,你下载的图像应该是视网膜大小。从中生成非视网膜图像并保存在文档库中

这是如何缩放视网膜图像并将其保存在文档库中的示例。要获取非视网膜大小,您可以手动缩放它,然后保存它。下面是示例代码:

UIImage *image = [UIImage imageNamed:@"yourRetinaImage@2x.png"];

// set non-retina size from current image
CGSize size = CGSizeMake(image.size.width / 2., image.size.height / 2.);


/** scale the image */

UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


/** save scaled image */

NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// save with same name but without suffix "@2x"
NSString *filePath = [NSString stringWithFormat:@"%@/%@", basePath, @"nonRetinaImage"];

@try {
    [UIImagePNGRepresentation(scaledImage) writeToFile:filePath options:NSAtomicWrite error:nil];
} @catch (NSException *exception) {
    NSLog(@"error while saving non-retina image with exception %@", exception);
}

谢谢Noval,但如果你打算将视网膜缩放为非视网膜,那么你将无法获得视网膜图像中显示的质量。Like 100X100在视网膜上显示为50X50。我猜它不是缩小的,而是使用了某种压缩技术?上面的代码所做的是缩小它的规模,我想这会降低质量。我得到的图像名为
yourRetinaImage@2x.png
。我将图像大小调整为一半,并使用不同的名称保存图像