2

我正在使用 Brad Larson 的 GPUImage 并基于 GoBackSpaces 的 DLCImagePickerController 为 iPhone 构建一个照片应用程序(到目前为止它帮助我学习了很多东西),但我坚持使用我希望应用程序具有的功能之一. 我有两组案例:

案例A:一组代表“胶卷”的盒子(Filter Cases) 案例B:一组代表“相机镜头”的盒子(Len Cases)

在每种情况下,我都有 GPUImageFilterGroups。我希望用户能够从案例 A 中选择一个过滤器,然后能够在同一个静态图片上应用案例 B 中的另一个过滤器。所以,简而言之,在用户从案例 A 中选择一个过滤器后,静态图片会根据选择的过滤器发生变化,然后我想在修改后的静态图片上应用案例 B 中的另一个过滤器。我尝试了几种不同的方法,但我没有运气。从案例 B 中选择过滤器后,StaticPicture 消失。我需要保留一些东西吗?任何帮助将不胜感激。这是我的代码:

filter = [[GPUImageRGBFilter alloc] init];
lens = [[GPUImageRGBFilter alloc] init];

-(void) filterClicked:(UIButton *) sender {
for(UIView *view in self.filterScrollView.subviews){
    if([view isKindOfClass:[UIButton class]]){
        [(UIButton *)view setSelected:NO];
    }
}

[sender setSelected:YES];
[self removeAllTargets];

selectedFilter = sender.tag;
[self setFilter:sender.tag];
[self prepareFilter];
}

-(void) lensClicked:(UIButton *) sender {
for(UIView *view in self.lensScrollView.subviews){
    if([view isKindOfClass:[UIButton class]]){
        [(UIButton *)view setSelected:NO];
    }
}
[sender setSelected:YES];
selectedLens = sender.tag;
[self setLens:sender.tag];
[self prepareLens];
}

-(void) setFilter:(int) index {
switch (index) {

case 1:{

filter = [[GPUImageFilterGroup alloc] init];

UIImage *inputImage23 = [UIImage imageNamed:@"vignette.png"];
sourcePicture5 = [[GPUImagePicture alloc] initWithImage:inputImage23 smoothlyScaleOutput:YES];
GPUImageOverlayBlendFilter * overlay23 = [[GPUImageOverlayBlendFilter alloc] init];
[(GPUImageFilterGroup *)filter addFilter:overlay23];
[sourcePicture5 addTarget:overlay23 atTextureLocation:1];

[sourcePicture5 processImage];

UIImage *inputImage21 = [UIImage imageNamed:@"frame.png"];
sourcePicture6 = [[GPUImagePicture alloc] initWithImage:inputImage21 smoothlyScaleOutput:YES];
GPUImageNormalBlendFilter * overlay21 = [[GPUImageNormalBlendFilter alloc] init];
[(GPUImageFilterGroup *)filter addFilter:overlay21];
[sourcePicture6 addTarget:overlay21 atTextureLocation:1];

[sourcePicture6 processImage];

GPUilforddelta100 *ilford = [[GPUilforddelta100 alloc] init];
[(GPUImageFilterGroup *)filter addFilter:ilford];

[overlay23 addTarget:overlay21];
[overlay21 addTarget:ilford];

[(GPUImageFilterGroup *)filter setInitialFilters:[NSArray arrayWithObject:overlay23]];
[(GPUImageFilterGroup *)filter setTerminalFilter:ilford];
} break;

case 2: {
filter = [[GPUImageFilterGroup alloc] init];

UIImage *inputImage1 = [UIImage imageNamed:@"trixgrain.png"];
        sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage1 smoothlyScaleOutput:YES];
GPUImageMultiplyBlendFilter * overlay1 = [[GPUImageMultiplyBlendFilter alloc] init];
[(GPUImageFilterGroup *)filter addFilter:overlay1];
[sourcePicture addTarget:overlay1 atTextureLocation:1];

[sourcePicture processImage];

UIImage *inputImage2 = [UIImage imageNamed:@"trixfeel.png"];
sourcePicture3 = [[GPUImagePicture alloc] initWithImage:inputImage2 smoothlyScaleOutput:YES];
GPUImageOverlayBlendFilter * overlay2 = [[GPUImageOverlayBlendFilter alloc] init];
[(GPUImageFilterGroup *)filter addFilter:overlay2];
[sourcePicture3 addTarget:overlay2 atTextureLocation:1];

[sourcePicture3 processImage];

UIImage *inputImage18 = [UIImage imageNamed:@"frame.png"];
sourcePicture2 = [[GPUImagePicture alloc] initWithImage:inputImage18 smoothlyScaleOutput:YES];
GPUImageNormalBlendFilter * overlay18 = [[GPUImageNormalBlendFilter alloc] init];
[(GPUImageFilterGroup *)filter addFilter:overlay18];
[sourcePicture2 addTarget:overlay18 atTextureLocation:1];

[sourcePicture2 processImage];

GPUTrix400 *trix400 = [[GPUTrix400 alloc] init];
[(GPUImageFilterGroup *)filter addFilter:trix400];

[overlay2 addTarget:overlay1];
[overlay1 addTarget:overlay18];
[overlay18 addTarget:trix400];

[(GPUImageFilterGroup *)filter setInitialFilters:[NSArray arrayWithObject:overlay2]];
[(GPUImageFilterGroup *)filter setTerminalFilter:trix400];
} break;

default:
filter = [[GPUImageRGBFilter alloc] init];
break;
}
}

-(void) setLens:(int) index {
switch (index) {

case 1:{

lens = [[GPUImageFilterGroup alloc] init];

UIImage *inputImage1 = [UIImage imageNamed:@"lightleak.png"];
sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage1 smoothlyScaleOutput:YES];
GPUImageOverlayBlendFilter * overlay1 = [[GPUImageOverlayBlendFilter alloc] init];
[(GPUImageFilterGroup *)lens addFilter:overlay1];
[sourcePicture addTarget:overlay1 atTextureLocation:1];

[sourcePicture processImage];

UIImage *inputImage18 = [UIImage imageNamed:@"holgaframe.png"];
sourcePicture2 = [[GPUImagePicture alloc] initWithImage:inputImage18 smoothlyScaleOutput:YES];
GPUImageNormalBlendFilter * overlay18 = [[GPUImageNormalBlendFilter alloc] init];
[(GPUImageFilterGroup *)lens addFilter:overlay18];
[sourcePicture2 addTarget:overlay18 atTextureLocation:1];

[sourcePicture2 processImage];

GPUholgaroid *holgaroid = [[GPUholgaroid alloc] init];
[(GPUImageFilterGroup *)lens addFilter:holgaroid];

[holgaroid addTarget:overlay1];
[overlay1 addTarget:overlay18];

[(GPUImageFilterGroup *)lens setInitialFilters:[NSArray arrayWithObject:holgaroid]];
[(GPUImageFilterGroup *)lens setTerminalFilter:overlay18];

} break;

case 2:{

lens = [[GPUImageFilterGroup alloc] init];

UIImage *inputImage1 = [UIImage imageNamed:@"dianafeel.png"];
sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage1 smoothlyScaleOutput:YES];
GPUImageOverlayBlendFilter * overlay1 = [[GPUImageOverlayBlendFilter alloc] init];
[(GPUImageFilterGroup *)lens addFilter:overlay1];
[sourcePicture addTarget:overlay1 atTextureLocation:1];

[sourcePicture processImage];

UIImage *inputImage18 = [UIImage imageNamed:@"dianaframe.png"];
sourcePicture2 = [[GPUImagePicture alloc] initWithImage:inputImage18 smoothlyScaleOutput:YES];
GPUImageNormalBlendFilter * overlay18 = [[GPUImageNormalBlendFilter alloc] init];
[(GPUImageFilterGroup *)lens addFilter:overlay18];
[sourcePicture2 addTarget:overlay18 atTextureLocation:1];

[sourcePicture2 processImage];

GPUdianaf *dianaf = [[GPUdianaf alloc] init];
[(GPUImageFilterGroup *)lens addFilter:dianaf];

[dianaf addTarget:overlay1];
[overlay1 addTarget:overlay18];

[(GPUImageFilterGroup *)lens setInitialFilters:[NSArray arrayWithObject:dianaf]];
[(GPUImageFilterGroup *)lens setTerminalFilter:overlay18];

} break;

    default:
        lens = [[GPUImageRGBFilter alloc] init];
        break;
}
}

-(void) prepareFilter {
if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
    isStatic = YES;
}

if (!isStatic) {
    [self prepareLiveFilter];
} else {
    [self prepareStaticFilter];
}
}

-(void) prepareLiveFilter {

[filter removeAllTargets];
[cropFilter removeAllTargets];
[blurFilter removeAllTargets];

[stillCamera addTarget:cropFilter];
[cropFilter addTarget:filter];
if (hasBlur) {
    [filter addTarget:blurFilter];
    [blurFilter addTarget:self.imageView];
} else {
    [filter addTarget:self.imageView];
}

//GPUImage issue with new code, do not use until further improvement
//[filter prepareForImageCapture];

}

-(void) prepareStaticFilter {

if (!staticPicture) {
[self performSelector:@selector(switchToLibrary:) withObject:nil afterDelay:3.5];
}

[stillCamera removeAllTargets];
[staticPicture removeAllTargets];
[staticPicture addTarget:filter];
if (hasBlur) {
[filter addTarget:blurFilter];
[blurFilter addTarget:self.imageView];
} else {
    [filter addTarget:self.imageView];
}

GPUImageRotationMode imageViewRotationMode = kGPUImageNoRotation;
switch (staticPictureOriginalOrientation) {
case UIImageOrientationLeft:
imageViewRotationMode = kGPUImageRotateLeft;
break;
case UIImageOrientationRight:
imageViewRotationMode = kGPUImageRotateRight;
break;
case UIImageOrientationDown:
imageViewRotationMode = kGPUImageRotate180;
break;
default:
imageViewRotationMode = kGPUImageNoRotation;
break;
}

[self.imageView setInputRotation:imageViewRotationMode atIndex:0];

[staticPicture processImage];
}

-(void) prepareLens {
if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
    isStatic = YES;
}

if (!isStatic) {
    [self prepareLiveLens];
} else {
    [self prepareStaticLens];
}
}

-(void) prepareLiveLens {

[stillCamera addTarget:lens];
[lens addTarget:self.imageView];

//GPUImage issue with new code, do not use until further improvement
//[lens prepareForImageCapture];

}

-(void) prepareStaticLens {

if (!staticPicture) {
[self performSelector:@selector(switchToLibrary:) withObject:nil afterDelay:3.5];
}

[staticPicture addTarget:lens];
[lens addTarget:self.imageView];

GPUImageRotationMode imageViewRotationMode = kGPUImageNoRotation;
switch (staticPictureOriginalOrientation) {
case UIImageOrientationLeft:
imageViewRotationMode = kGPUImageRotateLeft;
break;
case UIImageOrientationRight:
imageViewRotationMode = kGPUImageRotateRight;
break;
case UIImageOrientationDown:
imageViewRotationMode = kGPUImageRotate180;
break;
default:
imageViewRotationMode = kGPUImageNoRotation;
break;
}

[self.imageView setInputRotation:imageViewRotationMode atIndex:0];

[staticPicture processImage];
}

-(void) removeAllTargets {
[filter removeAllTargets];
[lens removeAllTargets];
[sourcePicture removeAllTargets];
[sourcePicture2 removeAllTargets];
[sourcePicture3 removeAllTargets];
[stillCamera removeAllTargets];
[staticPicture removeAllTargets];
[cropFilter removeAllTargets];
[blurFilter removeAllTargets];
}

-(void) prepareForCapture {
[stillCamera.inputCamera lockForConfiguration:nil];
if(self.flashToggleButton.selected &&
[stillCamera.inputCamera hasTorch]){
[stillCamera.inputCamera setTorchMode:AVCaptureTorchModeOn];
[self performSelector:@selector(captureImage)
               withObject:nil
               afterDelay:0.50];
}else{
    [self captureImage];
}
}

-(void)captureImage {
[lens forceProcessingAtSize:CGSizeMake (1936, 1936)];
[staticPicture addTarget:lens];
[stillCamera capturePhotoAsImageProcessedUpToFilter:lens
                              withCompletionHandler:^(UIImage *processed, NSError *error) {
                                  isStatic = YES;
                                  runOnMainQueueWithoutDeadlocking(^{
                                      @autoreleasepool {
                                          [stillCamera.inputCamera unlockForConfiguration];
                                          [stillCamera stopCameraCapture];
                                          [self removeAllTargets];
                                          staticPicture = [[GPUImagePicture alloc] initWithImage:processed smoothlyScaleOutput:NO];
                                          staticPictureOriginalOrientation = processed.imageOrientation;

                                          [self prepareFilter];
                                          [self.retakeButton setHidden:NO];
                                          [self.photoCaptureButton setTitle:@"Done" forState:UIControlStateNormal];
                                          [self.photoCaptureButton setImage:nil forState:UIControlStateNormal];
                                          [self.photoCaptureButton setEnabled:YES];
                                          if(![self.filtersToggleButton isSelected]){
                                              [self showFilters];
                                          }
                                      }
                                  });
                              }];
}

-(IBAction) takePhoto:(id)sender{
[self.photoCaptureButton setEnabled:NO];

if (!isStatic) {
    isStatic = YES;

    [self.libraryToggleButton setHidden:YES];
    [self.cameraToggleButton setEnabled:NO];
    [self.flashToggleButton setEnabled:NO];
    [self prepareForCapture];

} else {

    GPUImageOutput<GPUImageInput> *processUpTo;

    if (hasBlur) {
        processUpTo = blurFilter;
    } else {
        processUpTo = lens;
    }

    [staticPicture processImage];

    UIImage *currentFilteredVideoFrame = [processUpTo imageFromCurrentlyProcessedOutputWithOrientation:staticPictureOriginalOrientation];

    NSDictionary *info = [[NSDictionary alloc] initWithObjectsAndKeys:
                          UIImageJPEGRepresentation(currentFilteredVideoFrame, self.outputJPEGQuality), @"data", nil];
    [self.delegate imagePickerController:self didFinishPickingMediaWithInfo:info];
}
}

-(void) dealloc {
[self removeAllTargets];
stillCamera = nil;
cropFilter = nil;
filter = nil;
blurFilter = nil;
sourcePicture = nil;
sourcePicture2 = nil;
sourcePicture3 = nil;
staticPicture = nil;
self.blurOverlayView = nil;
self.focusView = nil;
}

提前感谢菜鸟对这个问题的任何解释或帮助。

4

1 回答 1

2

您可以使用以下链接:

https://github.com/BradLarson/GPUImage/issues/112

谢谢

于 2013-05-20T13:10:22.323 回答