简体   繁体   English

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

[英]Apply filter to current image index in UIScrollView GPUImage

Im currently deploying a ImageScroller by UIScrollView, and it contain a filter toolbar at the bottom as you can see on the image. 我目前正在通过UIScrollView部署ImageScroller,它在底部包含一个过滤器工具栏,您可以在图像上看到。

在此处输入图片说明

But im facing a problem is that when i press the button, the filters is only apply to the last image as example below: 但是我面临的一个问题是,当我按下按钮时,过滤器仅适用于最后一个图像,如下所示:

Gif Example Gif示例

Gif示例

So please anyone can help me to solve this one, my expected output is the filter is applied to the current displaying image, not only the last one. 因此,请任何人可以帮助我解决这一问题,我的预期输出是将滤镜应用于当前显示的图像,而不仅仅是最后一个。 Here i would attach my code for your reference and thank you so much. 在这里,我附上我的代码供您参考,非常感谢。

@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];
    }
}

Just filter the last shown image and when filter image when scrollview scrolled to another image . 只需过滤最后显示的图像,并在滚动视图滚动to another image时过滤to another image

remember to add UIScrollViewDelegate 记得添加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];
}

Updated If you want to filter current shown image, not need to apply UIScrollViewDelegate: 已更新如果要过滤当前显示的图像,则无需应用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];
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM