Kev*_*tre 15 cocoa-touch objective-c ios
我有一个应用程序从一个拉取图像NSURL
.是否可以通知应用程序它们是视网膜('@ 2x')版本(图像是视网膜分辨率)?我目前有以下内容,但图像在更高分辨率的显示屏上显示为像素化:
NSURL *url = [NSURL URLWithString:self.imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
self.pictureImageView.image = image;
Run Code Online (Sandbox Code Playgroud)
Car*_*len 17
在将UIImage添加到图像视图之前,需要重新缩放UIImage.
NSURL *url = [NSURL URLWithString:self.imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
CGFloat screenScale = [UIScreen mainScreen].scale;
if (image.scale != screenScale)
image = [UIImage imageWithCGImage:image.CGImage scale:screenScale orientation:image.imageOrientation];
self.pictureImageView.image = image;
Run Code Online (Sandbox Code Playgroud)
最好避免对刻度值进行硬编码,从而避免使用UIScreen调用.请参阅苹果的文档UIImage
的scale
属性有关,为什么这是必要的详细信息.
这也是最好避免使用NSData
的-dataWithContentsOfURL:
方法(除非你的代码是在后台线程中运行),因为它使用不能被监视或取消了同步网络通话.您可以在Apple技术问答中详细了解同步网络的难点以及避免它的方法.
Rya*_*yan 11
尝试使用 imageWithData:scale:
(iOS 6及更高版本)
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData scale:[[UIScreen mainScreen] scale]];
Run Code Online (Sandbox Code Playgroud)
您需要在UIImage上设置比例.
UIImage* img = [[UIImage alloc] initWithData:data];
CGFloat screenScale = [UIScreen mainScreen].scale;
if (screenScale != img.scale) {
img = [UIImage imageWithCGImage:img.CGImage scale:screenScale orientation:img.imageOrientation];
}
Run Code Online (Sandbox Code Playgroud)
文档说要小心地以相同的比例构建所有的UIImages,否则你可能会出现奇怪的显示问题,其中显示的是半尺寸,双倍尺寸,半分辨率等等.为避免这一切,请在视网膜分辨率下加载所有UIImages.资源将自动以正确的比例加载.对于从URL数据构造的UIImages,您需要进行设置.