簡體   English   中英

iOS裁剪UIImage的一部分

[英]iOS crop one part of UIImage

我正在使用Avfoundation捕獲視頻,我想裁剪捕獲的UIImage的一部分。 我正在使用兩個UIImageView。 布局

這是我創建視圖的代碼。

    -(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中的圖像。 我錯過了什么嗎?

謝謝

讓我看看我是否得到了這個。
您想要在大圖像視圖中顯示圖像,然后您想要剪切該圖像的一部分並以某種放大鏡顯示它。
當你在圖像視圖2中看到大圖像的不同矩形時,這是正確的嗎?
我猜這個問題是由於圖像視圖1內容模式(和/或圖像視圖內容模式)。

圖像視圖使用其contentMode屬性和圖像本身的配置來確定如何顯示圖像。 最好指定尺寸與圖像視圖尺寸完全匹配的圖像,但圖像視圖可以縮放圖像以適合所有或部分可用空間。 如果圖像視圖本身的大小發生變化,它會根據需要自動縮放圖像。

另一個問題可能是您用於裁剪圖像的框架,這是因為視圖框架表示視圖的坐標與其超視圖相關。
在您的情況下,只有當圖像視圖1占據其超級視圖的所有邊界並且與圖像視圖2共享相同的超級視圖時,該匹配才是正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM