简体   繁体   English

iOS裁剪UIImage的一部分

[英]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. 我正在使用Avfoundation捕获视频,我想裁剪捕获的UIImage的一部分。 I'm using two UIImageView. 我正在使用两个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 我想在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. 由于某种原因,裁剪后的图像永远不会是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? 当你在图像视图2中看到大图像的不同矩形时,这是正确的吗?
I guess that the problem is due to image view 1 content mode (and or image view content mode. 我猜这个问题是由于图像视图1内容模式(和/或图像视图内容模式)。

An image view uses its contentMode property and the configuration of the image itself to determine how to display the image. 图像视图使用其contentMode属性和图像本身的配置来确定如何显示图像。 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. 在您的情况下,只有当图像视图1占据其超级视图的所有边界并且与图像视图2共享相同的超级视图时,该匹配才是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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