在 UITableViewCell 中显示必须调整大小的图像

发布于 2024-12-04 02:21:43 字数 317 浏览 1 评论 0原文

您好,我目前在 CoreData 模型中有一个小图像(大约 100x160)作为 NSData 属性。

我在 TableView 中显示所有实体。单个Cell中的UIImageView只有50x80的大小。只是将图像放入此框架中看起来有点卵石状。

在我的 tableViewCell 中显示此图像的最佳解决方案是什么?在我的 cellForRowAtIndexPath 中动态调整它的大小?可能这会导致我的桌面视图变得有点滞后。

在创建时调整它的大小并将其保存在我的 coredata 实体中(或者可能保存在磁盘上)?

谢谢你!如果有不清楚的地方请发表评论

Hi so i currently have a small image (about 100x160) as a NSData Attribute in my CoreData model.

i display all entities in a TableView. The UIImageView in a single Cell has only a size of 50x80. just dropping the image into this frame looks a bit pebbly.

what would be the best solution to display this image in my tableViewCell? resize it on-the-fly in my cellForRowAtIndexPath? probably this will lead up my tableview to become a bit laggy.

resize it on create and save it in my coredata entity (or probably on disk)?

thank you! please leave a comment if something is unclear

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2024-12-11 02:21:43

为此,您必须裁剪/调整图像大小。以下是根据所需帧裁剪图像的代码。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // do something......
        UIImage *img = [UIImage imageWithData:(nsdata)]; // nsdata will be your image data as you specified.
        // To crop Image
        UIImage *croppedImage = [self imageByCropping:img] toRect:CGRectMake(10, 10, 50, 80)];

       // To resize image
        UIImage *resizedImage = [self resizeImage:img width:50 height:80];
    }

裁剪图像:

    - (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
       CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
       UIImage *cropped = [UIImage imageWithCGImage:imageRef];
       return cropped;
    }

调整图像大小:

    -(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height 
    {
        CGImageRef imageRef = [image CGImage];
        CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
        alphaInfo = kCGImageAlphaNoneSkipLast;
        CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
        CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
        CGImageRef ref = CGBitmapContextCreateImage(bitmap);
        UIImage *result = [UIImage imageWithCGImage:ref];   
        return result;  
    }

您可以使用任何一种方法。

For that you have to crop/resize the image. Following is the code to crop the image as per the required frame.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // do something......
        UIImage *img = [UIImage imageWithData:(nsdata)]; // nsdata will be your image data as you specified.
        // To crop Image
        UIImage *croppedImage = [self imageByCropping:img] toRect:CGRectMake(10, 10, 50, 80)];

       // To resize image
        UIImage *resizedImage = [self resizeImage:img width:50 height:80];
    }

Crop Image:

    - (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
       CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
       UIImage *cropped = [UIImage imageWithCGImage:imageRef];
       return cropped;
    }

Resize Image:

    -(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height 
    {
        CGImageRef imageRef = [image CGImage];
        CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
        alphaInfo = kCGImageAlphaNoneSkipLast;
        CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
        CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
        CGImageRef ref = CGBitmapContextCreateImage(bitmap);
        UIImage *result = [UIImage imageWithCGImage:ref];   
        return result;  
    }

You can go with either of ways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文