嗨所以我目前在我的CoreData模型中有一个小图像(大约100x160)作为NSData属性。
我在TableView中显示所有实体。单个Cell中的UIImageView只有50x80的大小。只是把图像放到这个框架看起来有点像石头。
在tableViewCell中显示此图像的最佳解决方案是什么?在我的cellForRowAtIndexPath中动态调整大小?可能这会导致我的桌面视图变得有点滞后。
在创建时调整它并将其保存在我的coredata实体(或可能在磁盘上)?
谢谢你!如果不清楚,请发表评论答案 0 :(得分:0)
为此你必须裁剪/调整图像大小。以下是根据所需帧裁剪图像的代码。
- (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;
}
你可以采取任何一种方式。