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;
}
答案 0 :(得分:2)
我找到了问题的解决方案。实际上我正在将滤镜应用于不需要的完整/大缩略图。预览过滤器图片视图,大小为50x50。所以我首先缩小缩略图图像的大小(保持纵横比),然后对其应用滤镜。现在它的工作正常。请在下面的代码中查看我的编辑内容:
// 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;
}