繁体   English   中英

当图像尺寸太大时,Tableview单元摇动

[英]Tableview cell shaking when image size is too large

我必须在固定大小的区域(例如100 * 100 px)中显示图像,但实际图像大小太大(例如400 * 450),问题是当我在100 * 100 px imageview中设置大图像时,滚动会产生单元抖动

我使用这行代码来创建一个新的UIImage,该UIImage可以缩放。 设置比例和方向参数以实现所需的效果。 第一行代码仅捕获图像。

// grab the original image
UIImage *originalImage = [UIImage imageNamed:@"myImage.png"];
// scaling set to 2.0 makes the image 1/2 the size. 
UIImage *scaledImage = 
            [UIImage imageWithCGImage:[originalImage CGImage] 
                          scale:(originalImage.scale * 2.0)
                             orientation:(originalImage.imageOrientation)];

最简单的方法是设置UIImageView的框架并将contentMode设置为调整大小选项之一。

或者,如果您确实需要调整图像大小,则可以使用此实用程序方法:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM