Search code examples
iosobjective-ciphonecameragpuimage

Objevtive - C : Save zoomed caputre image from camera with GPUImage?


I have done zooming functionality in camera with GPUImage. But when I capture image from camera with zoom and save it but still it save as normal pict(no zooming found). I want in whichever mode I capture image that must be saved in album. How can I solve this problem Any suggestion will be great. Thanks guys. My code :

- (void)viewDidLoad {
    [super viewDidLoad];
    self.library = [[ALAssetsLibrary alloc] init];
    [self setViewLayOut];
    [self setupFilter];
    [self setZoomFunctionlityOnCamera];

}


- (void)setupFilter;
{
    videoCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    switch (filterType)
    {
        case GPUIMAGE_COLORINVERT:
        {
            self.title = @"Color Negative";
            filter = [[GPUImageColorInvertFilter alloc] init];
        };
        break;
        case GPUIMAGE_GRAYSCALE:
        {
            self.title = @"Black and White Positive";
            filter = [[GPUImageGrayscaleFilter alloc] init];
        };
        break;

        default: filter = [[GPUImageFilter alloc] init];
        self.title = @"Color Positive";
        break;
    }
    videoCamera.runBenchmark = YES;
    filterView =  (GPUImageView *)cameraView;
    [filter addTarget:filterView];
    [videoCamera addTarget:filter];
    [videoCamera startCameraCapture];
}



    - (IBAction)clickPhotoBtn:(id)sender {
    if (!isCameraPermissionAccessed) {
        [self showAccessDeinedMessage :@"Camera permission denied" withMessage:@"To enable, please go to settings and allow camera permission for this app."];
        return;
    }
    [videoCamera capturePhotoAsJPEGProcessedUpToFilter:filter withCompletionHandler:^(NSData *processedJPEG, NSError *error){
        if (error!=nil)
        {
            [self showErrorMessage:@"Unable to capture image" ];
            return ;
        }

        else {
             UIImage *image = [UIImage imageWithData:processedJPEG];
            if (filterType == GPUIMAGE_GRAYSCALE) {
            GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:image];
            GPUImageColorInvertFilter *stillImageFilter = [[GPUImageColorInvertFilter alloc] init];
            [stillImageSource addTarget:stillImageFilter];
            [stillImageFilter useNextFrameForImageCapture];
            [stillImageSource processImage];
            UIImage *currentFilteredVideoFrame = [stillImageFilter imageFromCurrentFramebuffer];
            UIImageWriteToSavedPhotosAlbum(currentFilteredVideoFrame, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

            }
        else{
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
            }
        }

     }];
   }

Solution

  • Use below code it may helpful to you

    +(UIImage*)croppedImageWithImage:(UIImage *)image zoom:(CGFloat)zoom
    {
        CGFloat zoomReciprocal = 1.0f / zoom;
    
        CGPoint offset = CGPointMake(image.size.width * ((1.0f - zoomReciprocal) / 2.0f), image.size.height * ((1.0f - zoomReciprocal) / 2.0f));
        CGRect croppedRect = CGRectMake(offset.x, offset.y, image.size.width * zoomReciprocal, image.size.height * zoomReciprocal);
    
        CGImageRef croppedImageRef = CGImageCreateWithImageInRect([image CGImage], croppedRect);
    
        UIImage* croppedImage = [[UIImage alloc] initWithCGImage:croppedImageRef scale:[image scale] orientation:[image imageOrientation]];
    
        CGImageRelease(croppedImageRef);
    
        return croppedImage;
    }