简体   繁体   中英

iOS crop one part of UIImage

I'm using Avfoundation to capture video and I would like to crop one part of the captured UIImage. I'm using two UIImageView. layout

Here is my code to create the view.

    -(void)addCamToView
    {
      AVCaptureSession *session = [[AVCaptureSession alloc]init];
      session.sessionPreset = AVCaptureSessionPresetPhoto;

       AVCaptureDevice *device =
      [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
      AVCaptureDeviceInput *input = [AVCaptureDeviceInput        deviceInputWithDevice:device error:nil];

    [session addInput:input];
   AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    _stillImageOutput = [[AVCaptureStillImageOutput alloc]init];
    [session addOutput:output];
    [session addOutput:_stillImageOutput];
    output.videoSettings =
    @{ (NSString *)kCVPixelBufferPixelFormatTypeKey :  @(kCVPixelFormatType_32BGRA) };
   AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    previewLayer.frame = _imv1.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResize;

    CALayer *ca_imv2 = _imv2.layer;
    CALayer *ca_mainImv = _imv1.layer;

    [previewLayer addSublayer:ca_mainImv];
    [previewLayer addSublayer:ca_imv2];

    [self.view.layer addSublayer:previewLayer];

    [session startRunning];
}

When user press the capture button I'm running this code to capture the image :

    -(void) Capture: (UIImageView *) 
    {
        AVCaptureConnection *videoConnection = nil;
        for (AVCaptureConnection *connection in _stillImageOutput.connections)
        {
            for (AVCaptureInputPort *port in [connection inputPorts])
            {
                if ([[port mediaType] isEqual:AVMediaTypeVideo] )
                {
                    videoConnection = connection;
                    break;
                }
            }

            if (videoConnection) { break; }
        }
        [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer,NSError *error)
             {
                 NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                 UIImage *image = [[UIImage alloc] initWithData:imageData];

                 UIImage *croppedImv = [self crop:imv2.frame: image];

                 self.imv1.image = croppedImv;
             }];
}

Here is my code to crop the image. I would like to extract the part of the image in UIImageView2

- (UIImage *)crop:(CGRect)rect :(UIImage *)img {

    CGImageRef imageRef = CGImageCreateWithImageInRect(img.CGImage, rect);
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:img.scale orientation: img.imageOrientation];
    CGImageRelease(imageRef);
    return result;
}

For some reason the cropped image is never the image in UIImageView2. Am I missing something?

Thanks

Let me see if I got this.
You want to display your image in a big image view, then you want to cut a part of that image and show it in a sort of loupe.
At moment you are seeing a different rect of your big image inside the image view 2, is that correct?
I guess that the problem is due to image view 1 content mode (and or image view content mode.

An image view uses its contentMode property and the configuration of the image itself to determine how to display the image. It is best to specify images whose dimensions match the dimensions of the image view exactly, but image views can scale your images to fit all or some of the available space. If the size of the image view itself changes, it automatically scales the image as needed.

Another issue can be the frame that you are using to crop the image, this is because a view frame represents the coordinate of a view respect its superview.
In your case, that match can be correct only if image view 1 occupy all the bounds of its superview and share the same superview with image view 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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