gpt4 book ai didi

iphone - ios : set cell image dimension

转载 作者:行者123 更新时间:2023-11-29 04:16:01 25 4
gpt4 key购买 nike

我正在从我的网络服务器加载一组图像,以显示在右侧的单元格中。但是,图像的大小不同,因此显示列表时它们看起来不均匀。无论如何,我可以将图像设置为固定尺寸,例如 100 x 80 吗?

本节我的代码如下:

cell.lotImageView.image = [UIImage imageNamed:@"blankthumbnail.png"];
cell.lotImageView.clipsToBounds = YES;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
//load image from web server
NSString *strURL = [NSString stringWithFormat:@"%@/images/%@", user.url, lotPhoto[row]];
NSURL *url = [[NSURL alloc] initWithString:strURL ];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

dispatch_async(dispatch_get_main_queue(), ^{
// let's make sure the cell is still visible (i.e. hasn't scrolled off the screen)
mainTableCell *cell = (mainTableCell *)[tableView cellForRowAtIndexPath:indexPath];
if (cell)
{
cell.lotImageView.clipsToBounds = YES;
cell.lotImageView.image =image;
}
});
});

最佳答案

您可以通过以下方法获得所需的图像尺寸。您可以将以下函数放入应用程序委托(delegate)中,并可以在整个应用程序中使用。代码如下:

+(UIImage *) resizeImage:(UIImage *)orginalImage resizeSize:(CGSize)size {

CGFloat actualHeight = orginalImage.size.height;
CGFloat actualWidth = orginalImage.size.width;
if(actualWidth <= size.width && actualHeight<=size.height){
return orginalImage;
//NSLog(@"hi thassoods");
}
float oldRatio = actualWidth/actualHeight;
float newRatio = size.width/size.height;
if(oldRatio < newRatio){
oldRatio = size.height/actualHeight;
actualWidth = oldRatio * actualWidth;
actualHeight = size.height;
}
else {
oldRatio = size.width/actualWidth;
actualHeight = oldRatio * actualHeight;
actualWidth = size.width;
}
CGRect rect = CGRectMake(0.0,0.0,actualWidth,actualHeight);
UIGraphicsBeginImageContext(rect.size);
[orginalImage drawInRect:rect];
orginalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return orginalImage;
}

但是,如果您指定的新尺寸与图像的原始尺寸不成比例,那么您的图像分辨率可能无法保持正确。

祝一切顺利!!!

关于iphone - ios : set cell image dimension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com