CIFilter减慢滚动速度

时间:2014-03-26 23:31:34

标签: ios objective-c cifilter

我正在将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上快速滚动时,会加载图像和图像滤镜,但滚动时会经常出现颠簸或锯齿状。

1 个答案:

答案 0 :(得分:1)

将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;
    });
});