【问题标题】:iOS crop one part of UIImageiOS 裁剪 UIImage 的一部分
【发布时间】:2017-06-20 08:44:27
【问题描述】:

我正在使用 Avfoundation 来捕获视频,我想裁剪捕获的 UIImage 的一部分。 我正在使用两个 UIImageView。 layout

这是我创建视图的代码。

    -(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];
}

当用户按下捕获按钮时,我正在运行此代码来捕获图像:

    -(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;
             }];
}

这是我裁剪图像的代码。我想在 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;
}

由于某种原因,裁剪后的图像永远不会是 UIImageView2 中的图像。我错过了什么吗?

谢谢

【问题讨论】:

    标签: ios objective-c iphone uiimageview uiimage


    【解决方案1】:

    让我看看我有没有这个。
    您想在大图像视图中显示您的图像,然后您想剪切该图像的一部分并以某种放大镜显示它。
    目前,您在图像视图 2 中看到了大图像的不同矩形,对吗?
    我猜这个问题是由于图像视图 1 内容模式(和/或图像视图内容模式。

    图像视图使用其 contentMode 属性和 图像本身决定如何显示图像。最好是 指定尺寸与图像视图尺寸匹配的图像 确切地说,但是图像视图可以缩放您的图像以适应全部或部分 可用空间。如果图像视图本身的大小发生变化,它 根据需要自动缩放图像。

    另一个问题可能是您用于裁剪图像的框架,这是因为视图框架表示视图相对于其父视图的坐标。
    在您的情况下,只有当图像视图 1 占据其父视图的所有边界并与图像视图 2 共享相同的父视图时,该匹配才是正确的。

    【讨论】:

    • 感谢安德里亚的回答。这正是我想要做的。我的最终目标是对 imageView2 中捕获的图像进行一些操作。我在 imageView1 中显示它只是为了确保我裁剪了正确的部分。 Imageview1 是 imageView2 的父级,imageView1 是主 UIView 的子视图,与主 UIView 具有相同的框架。如何获得 UIimageView2 的正确坐标以便能够裁剪图像?
    • 仅在代码中无法在界面生成器中向 uiimageview 添加子视图。您是在界面生成器还是代码中创建界面?
    • 我在后面的代码中创建接口。我更改了代码以在 previewLayer 中添加 imv2,如下所示: CALayer *ca_imv2 = _imv2.layer; CALayer *ca_imv1= _imv1.layer; [previewLayer addSublayer:ca_imv1]; [previewLayer addSublayer:ca_imv2];我有同样的问题你是对的问题可能是因为如果我添加:_imv2.center = _imv1.center; imv2 不在 imv1 的中心,但我不明白我做错了什么,我想我错过了一些东西。谢谢
    • 你为什么要搞乱层?仅使用视图
    • 我在使用视图和固定位置时遇到了同样的问题。 CGImageRef imageRef = CGImageCreateWithImageInRect(imageToCrop.CGImage, rect); UIImage *result = [UIImage imageWithCGImage:imageRef scale:imageToCrop.scale 方向: imageToCrop.imageOrientation]; CGImageRelease(imageRef);返回的图像总是与正方形中的图像不同
    猜你喜欢
    • 2013-10-11
    • 2017-06-13
    • 2017-01-15
    • 2010-09-14
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多