1
votes

I am using GPUImage for applying video filters. But before applying filter to particular video, I am showing filter previews on the thumbnail image of video at bottom on UIScrollView. For this, I am using imageByFilteringImage: method of filter and I thought it causes memory pressure and crashes application. Please check my code below and suggest necessary changes :

// Get the thumnail image form video
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:self.movieURL options:nil];
    AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
    imageGenerator.appliesPreferredTrackTransform = true;
    UIImage *thumbailImage = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];

    CGFloat xLocation = 10;
    for (NSNumber *filterNumber in videoFilterList) {
        NSLog(@"filterNumber : %@",filterNumber);

        UILabel *filterNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(xLocation, 12, 50, 16)];
        filterNameLabel.backgroundColor = [UIColor clearColor];
        filterNameLabel.textAlignment = NSTextAlignmentCenter;
        filterNameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];

        UIImage *quickFilteredImage = thumbailImage;
        switch (tag) {
            case GPUIMAGE_SATURATION:
            {
                filter = nil;
                filter = [[GPUImageSaturationFilter alloc] init];
                [(GPUImageSaturationFilter *)filter setSaturation:0.0];
                quickFilteredImage = [filter imageByFilteringImage:thumbailImage];
                filterNameLabel.text = @"B&W";
            } break;
            case GPUIMAGE_SEPIA:
            {
                filter = nil;
                filter = [[GPUImageSepiaFilter alloc] init];
                quickFilteredImage = [filter imageByFilteringImage:thumbailImage];
                filterNameLabel.text = @"Sepia";
            } break;
            case GPUIMAGE_GRAYSCALE:
            {
                filter = nil;
                filter = [[GPUImageGrayscaleFilter alloc] init];
                quickFilteredImage = [filter imageByFilteringImage:thumbailImage];
                filterNameLabel.text = @"GrayScale"; break;
            }
        }
        [self.previewFiltersScrollView addSubview:filterNameLabel];

        UIImageView *previewFilterImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xLocation, -40, 50, 50)];
        previewFilterImageView.userInteractionEnabled = YES;
        previewFilterImageView.tag = tag;
        previewFilterImageView.contentMode = UIViewContentModeScaleAspectFit;
        previewFilterImageView.image = quickFilteredImage;

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(applyFilter:)];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        tapGestureRecognizer.numberOfTouchesRequired = 1;
        [previewFilterImageView addGestureRecognizer:tapGestureRecognizer];

        [self.previewFiltersScrollView addSubview:previewFilterImageView];

        tag++;
        xLocation += 60;
    } 
1

1 Answers

2
votes

I found the solution to my question. Actually I was applying filter to the full/big thumbnail image that was not needed. The preview filter image view as of size 50x50. So I first reduced the size of thumbnail image (with maintaining aspect ratio) and then applied filters to it. Now its working fine. Please see my edits in code below :

// Get the thumnail image form video
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:self.movieURL options:nil];
    AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
    imageGenerator.appliesPreferredTrackTransform = true;
    UIImage *thumbailImage = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];

// Compress the image with aspect fit
    CGSize aspectSize = CGSizeMake(100, 100);
    CGSize imageViewSize = CGSizeMake(100, 100);
    CGFloat aspect = tmpThumbnailImage.size.width / tmpThumbnailImage.size.height;
    if (imageViewSize.width / aspect <= imageViewSize.height) {
        aspectSize = CGSizeMake(imageViewSize.width, imageViewSize.width / aspect);
    } else {
        aspectSize = CGSizeMake(imageViewSize.height * aspect, imageViewSize.height);
    }
    UIGraphicsBeginImageContext(aspectSize);
    [tmpThumbnailImage drawInRect:CGRectMake(0, 0, aspectSize.width, aspectSize.height)];
    thumbailImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGFloat xLocation = 10;
    for (NSNumber *filterNumber in videoFilterList) {
        NSLog(@"filterNumber : %@",filterNumber);

     NSInteger tag = [filterNumber intValue];

        UILabel *filterNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(xLocation, 12, 50, 16)];
        filterNameLabel.backgroundColor = [UIColor clearColor];
        filterNameLabel.textAlignment = NSTextAlignmentCenter;
        filterNameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];

        UIImage *quickFilteredImage = thumbailImage;
        switch (tag) {
            case GPUIMAGE_SATURATION:
            {
                filter = nil;
                filter = [[GPUImageSaturationFilter alloc] init];
                [(GPUImageSaturationFilter *)filter setSaturation:0.0];
                quickFilteredImage = [filter imageByFilteringImage:thumbailImage];
                filterNameLabel.text = @"B&W";
            } break;
            case GPUIMAGE_SEPIA:
            {
                filter = nil;
                filter = [[GPUImageSepiaFilter alloc] init];
                quickFilteredImage = [filter imageByFilteringImage:thumbailImage];
                filterNameLabel.text = @"Sepia";
            } break;
            case GPUIMAGE_GRAYSCALE:
            {
                filter = nil;
                filter = [[GPUImageGrayscaleFilter alloc] init];
                quickFilteredImage = [filter imageByFilteringImage:thumbailImage];
                filterNameLabel.text = @"GrayScale"; break;
            }
        }
        [self.previewFiltersScrollView addSubview:filterNameLabel];

        UIImageView *previewFilterImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xLocation, -40, 50, 50)];
        previewFilterImageView.userInteractionEnabled = YES;
        previewFilterImageView.tag = tag;
        previewFilterImageView.contentMode = UIViewContentModeScaleAspectFit;
        previewFilterImageView.image = quickFilteredImage;

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(applyFilter:)];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        tapGestureRecognizer.numberOfTouchesRequired = 1;
        [previewFilterImageView addGestureRecognizer:tapGestureRecognizer];

        [self.previewFiltersScrollView addSubview:previewFilterImageView];

        xLocation += 60;
    }