簡體   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