gpt4 book ai didi

ios - CIFilter 减慢滚动速度

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:25 26 4
gpt4 key购买 nike

我正在将 CIFilter 应用于 UIImage,但它会减慢我的 UITableView 上的滚动速度。有什么我可以做的吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"tweetCell";

TweetCell *cell = (TweetCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TweetCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

cell.tweet.text = _tweets[indexPath.row][@"text"];

NSDictionary *tweetData = _tweets[indexPath.row];
NSString *profilePhotoURL = _profilePhotos[tweetData[@"username"]];

UIImage *bgPicture = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:profilePhotoURL]]];

dispatch_async(dispatch_get_main_queue(), ^(void){

CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[gaussianBlurFilter setDefaults];
[gaussianBlurFilter setValue:[CIImage imageWithCGImage:[bgPicture CGImage]] forKey:kCIInputImageKey];
[gaussianBlurFilter setValue:@0.7 forKey:kCIInputRadiusKey];

CIImage *outputImage = [gaussianBlurFilter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGRect rect = [outputImage extent];

rect.origin.x += (rect.size.width - bgPicture.size.width ) / 2;
rect.origin.y += (rect.size.height - bgPicture.size.height) / 2;
rect.size = bgPicture.size;

CGImageRef cgimg = [context createCGImage:outputImage fromRect:rect];
UIImage *image = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);

cell.bgP.image = image;

});

return cell;
}

我的意思是我已经添加了使滚动效果更好的调度部分。但是,当我在 UITableView 上快速滚动时,图像和图像滤镜已加载,但在滚动时会出现频繁的颠簸或锯齿状。

最佳答案

使用 dispatch_async 到其他线程,然后在完成时更新。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[gaussianBlurFilter setDefaults];
[gaussianBlurFilter setValue:[CIImage imageWithCGImage:[bgPicture CGImage]] forKey:kCIInputImageKey];
[gaussianBlurFilter setValue:@0.7 forKey:kCIInputRadiusKey];

CIImage *outputImage = [gaussianBlurFilter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGRect rect = [outputImage extent];

rect.origin.x += (rect.size.width - bgPicture.size.width ) / 2;
rect.origin.y += (rect.size.height - bgPicture.size.height) / 2;
rect.size = bgPicture.size;

CGImageRef cgimg = [context createCGImage:outputImage fromRect:rect];
UIImage *image = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);
dispatch_async(dispatch_get_main_queue(), ^{
cell.bgP.image = image;
});
});

关于ios - CIFilter 减慢滚动速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22675275/

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