将过滤器应用于 UIScrollView GPUImage 中的当前图像索引

Apply filter to current image index in UIScrollView GPUImage

我目前正在通过 UIScrollView 部署一个 ImageScroller,它在底部包含一个过滤器工具栏,如您在图像上所见。

但我面临的问题是,当我按下按钮时,滤镜仅适用于最后一张图片,如下例所示:

Gif 示例

所以请任何人帮助我解决这个问题,我的预期输出是滤镜应用于当前显示的图像,而不仅仅是最后一张。在这里,我将附上我的代码供您参考,非常感谢。

@interface EditImageVC ()
@end

@implementation EditImageVC


UIImageView *img;


- (void)viewDidLoad {
    [super viewDidLoad];
    int x=0;
    for (int i = 0; i < _scrollViewImageArray.count ; i++) {

        _picture = [_scrollViewImageArray objectAtIndex:i];
        img =[[UIImageView alloc]initWithFrame:CGRectMake(x,20,[[UIScreen mainScreen] bounds].size.width, 
        self.imgScrollView.frame.size.height)];
        img.image =_picture;
        x=x+[[UIScreen mainScreen] bounds].size.width;
        [_imgScrollView addSubview:img];
    }
    _imgScrollView.backgroundColor = [UIColor blackColor];
    _imgScrollView.contentSize=CGSizeMake(x, 
    _imgScrollView.frame.size.height);
    _imgScrollView.contentOffset=CGPointMake(0, 0);
    img.contentMode = UIViewContentModeScaleAspectFit;
}


-(void)viewDidLayoutSubviews{
     self.automaticallyAdjustsScrollViewInsets = NO;
}



 - (IBAction)filterAction:(UIButton *)sender {

    for (int i = 0; i < _scrollViewImageArray.count ; i++) {
        UIImage *picture = [_scrollViewImageArray objectAtIndex:i];
        GPUImageFilter *selectedFilter;
        switch (sender.tag) {
            case 0:
                selectedFilter = [[GPUImageSepiaFilter alloc] init];
                break;
            case 1:
                selectedFilter = [[GPUImageToonFilter alloc] init];
                break;
            default:
                break;
        }
        UIImage *filteredImage = [selectedFilter imageByFilteringImage:picture];
        [img setImage:filteredImage];
    }
}

只过滤最后显示的图像和滚动视图滚动时过滤图像to another image

记得添加 UIScrollViewDelegate

@interface EditImageVC : UIViewController <UIScrollViewDelegate>   {
}

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    imageViewsArray = [[NSMutableArray alloc] init];
    int x=0;
    for (int i = 0; i < _scrollViewImageArray.count ; i++) {

        _picture = [_scrollViewImageArray objectAtIndex:i];
        img =[[UIImageView alloc]initWithFrame:CGRectMake(x,20,[[UIScreen mainScreen] bounds].size.width,

        self.imgScrollView.frame.size.height)];
        img.image =_picture;
        [imageViewsArray addObject:img];
        x=x+[[UIScreen mainScreen] bounds].size.width;
        [self.imgScrollView addSubview:img];
    }
    self.imgScrollView.backgroundColor = [UIColor blackColor];
    self.imgScrollView.contentSize=CGSizeMake(x,
                                          self.imgScrollView.frame.size.height);
    self.imgScrollView.contentOffset=CGPointMake(0, 0);
    img.contentMode = UIViewContentModeScaleAspectFit;

    self.imgScrollView.delegate = self;
}

GPUImageFilter *selectedFilter = nil;

NSInteger currentShownIndex = 0;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger indexAfterScrolled = scrollView.contentOffset.x / scrollView.frame.size.width;
    if(indexAfterScrolled==currentShownIndex) return;
    currentShownIndex = indexAfterScrolled;
    if(selectedFilter!=nil) {
        UIImage *filteredImage = [selectedFilter imageByFilteringImage:[_scrollViewImageArray objectAtIndex:currentShownIndex]];
        [[imageViewsArray objectAtIndex:currentShownIndex] setImage:filteredImage];
    }
}

- (IBAction)filterAction:(UIButton *)sender {

    switch (sender.tag) {
        case 0:
            selectedFilter = [[GPUImageSepiaFilter alloc] init];
            break;
        case 1:
            selectedFilter = [[GPUImageToonFilter alloc] init];
            break;
        default:
            break;
    }
    UIImage *picture = [_scrollViewImageArray objectAtIndex:currentShownIndex];
    UIImage *filteredImage = [selectedFilter imageByFilteringImage:picture];
    [[imageViewsArray objectAtIndex:currentShownIndex] setImage:filteredImage];
}

已更新 如果要过滤当前显示的图像,不需要应用 UIScrollViewDelegate:

    - (void)viewDidLoad {
        [super viewDidLoad];
        imageViewsArray = [[NSMutableArray alloc] init];
        int x=0;
        for (int i = 0; i < _scrollViewImageArray.count ; i++) {

            _picture = [_scrollViewImageArray objectAtIndex:i];
            img =[[UIImageView alloc]initWithFrame:CGRectMake(x,20,[[UIScreen mainScreen] bounds].size.width,

            self.imgScrollView.frame.size.height)];
            img.image =_picture;
            [imageViewsArray addObject:img];
            x=x+[[UIScreen mainScreen] bounds].size.width;
            [self.imgScrollView addSubview:img];
        }
        self.imgScrollView.backgroundColor = [UIColor blackColor];
        self.imgScrollView.contentSize=CGSizeMake(x,
                                              self.imgScrollView.frame.size.height);
        self.imgScrollView.contentOffset=CGPointMake(0, 0);
        img.contentMode = UIViewContentModeScaleAspectFit;

        self.imgScrollView.delegate = self;
    }

    - (IBAction)filterAction:(UIButton *)sender {
        NSInteger objectAtIndex:currentShownIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
            GPUImageFilter *selectedFilter = nil;

        switch (sender.tag) {
            case 0:
                selectedFilter = [[GPUImageSepiaFilter alloc] init];
                break;
            case 1:
                selectedFilter = [[GPUImageToonFilter alloc] init];
                break;
            default:
                break;
        }
        UIImage *picture = [_scrollViewImageArray objectAtIndex:currentShownIndex];
        UIImage *filteredImage = [selectedFilter imageByFilteringImage:picture];
        [[imageViewsArray objectAtIndex:currentShownIndex] setImage:filteredImage];
    }