Question

I need to take overlay image without setting drawInRect. When i set drawInRect it gives output of setting size. I need to take picture with new size without using following code.

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


                                                         }];


}
Was it helpful?

Solution

Try this code :

    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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top