Ios 从web下载的图像比资源中的相同图像大

Ios 从web下载的图像比资源中的相同图像大,ios,objective-c,uiimage,uikit,Ios,Objective C,Uiimage,Uikit,在我的iOS应用程序中,我试图将一些图像从xcassets中删除到从Wordpress中下载。我将@2x图像从xcassets上传到wordpress,我正在运行iPhone7模拟器。然而,当我使用从服务器下载的图像时,我会得到一个更大的图像,而当我使用来自xcassets的完全相同的图像时 cell.imageView.image = [UIImage imageNamed:@"testImage"]; // is smaller in size than NSString *docume

在我的iOS应用程序中,我试图将一些图像从xcassets中删除到从Wordpress中下载。我将@2x图像从xcassets上传到wordpress,我正在运行iPhone7模拟器。然而,当我使用从服务器下载的图像时,我会得到一个更大的图像,而当我使用来自xcassets的完全相同的图像时

cell.imageView.image = [UIImage imageNamed:@"testImage"];

// is smaller in size than

NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [documentDir stringByAppendingString:[NSString stringWithFormat:@"/%@.png", @"testImage"]];
NSData *data = [NSData dataWithContentsOfFile:path];
cell.imageView.image = [UIImage imageWithData:data];
你知道是什么引起的吗?或者如何解决这个问题?

您写道:

我从xcassets上传了@2x图像

这就是原因

假设您的图像大小为400x400px(像素)。从资产目录加载时,iOS已经知道图像必须解释为200x200pt(点!),因为您在资产目录中将其定义为@2x

当使用代码iOS中的
imageWithData
方法将其从服务器加载到映像时,将其解释为@1x->400x400pt(点)

解决方案:

告诉图像使用比例参数加载图像的比例因子

[UIImage imageWithData:data scale:2.0f] 

请参见您的imageView的内容模式是什么?两者都未设置。所以我想他们是一样的。