简体   繁体   English

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

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

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

I used this single line of code to create a new UIImage which is scaled. 我使用这行代码来创建一个新的UIImage,该UIImage可以缩放。 Set the scale and orientation params to achieve what you want. 设置比例和方向参数以实现所需的效果。 The first line of code just grabs the image. 第一行代码仅捕获图像。

// 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)];

The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options. 最简单的方法是设置UIImageView的框架并将contentMode设置为调整大小选项之一。

Or you can use this utility method, if you actually need to resize an image: 或者,如果您确实需要调整图像大小,则可以使用此实用程序方法:

+ (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