【问题标题】:Capture overlay image without setting drawInRect在不设置 drawInRect 的情况下捕获覆盖图像
【发布时间】:2013-03-28 03:48:26
【问题描述】:

我需要在不设置 drawInRect 的情况下拍摄叠加图像。当我设置 drawInRect 它给出设置大小的输出。我需要在不使用以下代码的情况下以新尺寸拍照。

   - (void)captureStillImageWithOverlay:(UIImage*)overlay
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", [self stillImageOutput]);




    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
                                                             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                             if (exifAttachments) {
                                                                 NSLog(@"attachements: %@", exifAttachments);
                                                             } else {
                                                                 NSLog(@"no attachments");
                                                             }

                                                             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                             UIImage *image = [[UIImage alloc] initWithData:imageData];


                                                            CGSize imageSize = [image size];

                                                             CGSize overlaySize = [overlay size];

                                                             UIGraphicsBeginImageContext(imageSize);


                                                             [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];

                                                             CGFloat xScaleFactor = imageSize.width / 320;
                                                             CGFloat yScaleFactor = imageSize.height / 480;

                                                               [overlay drawInRect:CGRectMake(30 * xScaleFactor, 100 * yScaleFactor, overlaySize.width * xScaleFactor, overlaySize.height * yScaleFactor)]; // rect used in AROverlayViewController was (30,100,260,200)



                                                            // [overlay drawInRect:CGRectMake(30 * xScaleFactor, 100 * yScaleFactor, overlaySize.width * xScaleFactor, overlaySize.width* yScaleFactor)];





                                                             UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();

                                                             UIGraphicsEndImageContext();
                                                            // NSData * data = UIImagePNGRepresentation(image);
                                                            // [data writeToFile:@"foo.png" atomically:YES];





                                                              [self setStillImage:combinedImage];
                                                             [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];


                                                         }];


}

【问题讨论】:

    标签: iphone uiimage screen-capture camera-overlay


    【解决方案1】:

    试试这个代码:

        UIGraphicsBeginImageContext(self.view.bounds.size);
    // retrieve the current graphics context
        CGContextRef context = UIGraphicsGetCurrentContext();
    // render view into context
        [self.view.layer renderInContext:context];
    // create image from context
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        image=[self cropImage:image];
        UIGraphicsEndImageContext();
    
    
        - (UIImage *)cropImage:(UIImage *)oldImage 
        {
                CGSize imageSize = oldImage.size;
                UIGraphicsBeginImageContextWithOptions(CGSizeMake( imageSize.width,imageSize.height - 150),NO,0.);
                [oldImage drawAtPoint:CGPointMake( 0, -80) blendMode:kCGBlendModeCopy alpha:1.];
                UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                return croppedImage;
        }
    

    【讨论】:

    • 当我使用方向覆盖图像然后拍摄快照时,它会给出相同的大小
    • 您的代码提供透明背景。我需要背景中的相机视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多