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, изображения и фильтры изображений загружаются, но при прокрутке возникают частые удары или неровности.


person Community    schedule 26.03.2014    source источник


Ответы (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;
    });
});
person HoanNguyen    schedule 27.03.2014
comment
Спасибо, стало немного лучше, но лаги все равно есть. - person ; 27.03.2014