Ios 表视图UIImage呈现问题
我的tableView UIImages遇到了一个渲染问题,我想知道是否有人遇到过同样的问题,并且知道如何修复它 这是我的手机号码Ios 表视图UIImage呈现问题,ios,iphone,objective-c,Ios,Iphone,Objective C,我的tableView UIImages遇到了一个渲染问题,我想知道是否有人遇到过同样的问题,并且知道如何修复它 这是我的手机号码 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { cell.textLabel.text = exerciseDisplayName; cell.textLabel.numberOf
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.text = exerciseDisplayName;
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
[tableView setSeparatorInset:UIEdgeInsetsZero];
UtilityMethods *commonMethods = [[UtilityMethods alloc]init];
UIImage *rowImage = [commonMethods imageForRow:tempPlaceholder.bodyPart];
cell.imageView.image = rowImage;
return cell;
}
这是我的身高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 96;
}
表中的图像中有许多线条和曲线。我想知道是否有人知道任何UIImage属性,我可能需要将这些属性应用于我的图像以修复此问题。增加表中行的高度以增加表中行的高度为代价解决了问题。似乎有效的数字是128英寸高。当使用128时,波形就不那么明显了。现在我很确定这与iOS渲染图像的方式有关。我拍摄了这张图像,并使用Microsoft Paint将其大小调整为76x76,只是想看看是否会看到同样的问题,并且图像看起来很好,没有所有的扭曲。图像为.png格式。图像的原始大小为1024x1024。我只是根据需要向下调整了尺寸。如果有人对如何解决这个问题有任何建议,我将不胜感激。您需要将图像重新采样到所需的大小。在iOS设备上,在小空间中查看大图像看起来相当糟糕(大多数都是真的)。但是,如果您使用内置函数来创建适当大小的新UIImage,那么一切看起来都会好得多。显示时缩小UIImage的比例总是比创建一个适当大小的新图像并显示它更糟糕。方法如下:
-(UIImage*)图像按缩放和裁剪大小:(CGSize)targetSize
{
UIImage*sourceImage=self;
UIImage*newImage=nil;
CGSize imageSize=sourceImage.size;
CGFloat width=imageSize.width;
CGFloat height=图像大小.height;
CGFloat targetWidth=targetSize.width;
CGFloat targetHeight=targetSize.height;
CGFloat scaleFactor=0.0;
CGFloat scaledWidth=targetWidth;
CGFloat缩放高度=目标高度;
CGPoint thumbnailPoint=CGPointMake(0.0,0.0);
if(CGSizeEqualToSize(imageSize,targetSize)==否)
{
CGFloat宽度系数=目标宽度/宽度;
CGFloat高度系数=目标高度/高度;
如果(宽度因子>高度因子)
{
scaleFactor=widthFactor;//缩放以适应高度
}
其他的
{
scaleFactor=heightFactor;//缩放以适应宽度
}
scaledWidth=宽度*缩放因子;
缩放高度=高度*缩放因子;
//将图像居中
如果(宽度因子>高度因子)
{
thumbnailPoint.y=(targetLight-缩放高度)*0.5;
}
其他的
{
if(宽度系数<高度系数)
{
thumbnailPoint.x=(targetWidth-缩放宽度)*0.5;
}
}
}
UIGraphicsBeginImageContextWithOptions(targetSize,0,NO);//这将裁剪
CGRect thumbnailRect=CGRectZero;
thumbnailRect.origin=thumbnailPoint;
thumbnailRect.size.width=缩放宽度;
thumbnailRect.size.height=缩放高度;
[sourceImage drawInRect:thumbnailRect];
newImage=UIGraphicsGetImageFromCurrentImageContext();
if(newImage==nil)
{
NSLog(@“无法缩放图像”);
}
//弹出上下文以返回默认值
UIGraphicsSendImageContext();
返回新图像;
}
该函数比您所期望的要多一些,但您应该能够将其仅用于您需要的功能
确保使用
UIGraphicsBeginImageContextWithOptions
功能而不是UIGraphicsBeginImageContext
,以便正确处理视网膜显示,否则,这将使图像变得更加模糊,您将面临第二个问题。向下调整图像大小的唯一原因是为了节省空间。无论图像视图的大小(以像素为单位),加载到其中的图像应至少为该大小的2倍。我尝试将图像保留为1024x1024格式,用于表视图图像。当行的高度为96时,问题仍然存在。将行的高度增加到128可以在一定程度上缓解问题。
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
{
scaleFactor = widthFactor; // scale to fit height
}
else
{
scaleFactor = heightFactor; // scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
{
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
}
UIGraphicsBeginImageContextWithOptions(targetSize, 0, NO); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
{
NSLog(@"could not scale image");
}
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}