How to take Picture Programmatically in iphone App

2020-07-09 06:28发布

In iPhone App, Can we take pictures at some perticular time intervals programmatically by using iphone device camera ?

If yes then please let me know how we can take pictures programmatically in iPhone App?

Please Help and Suggest.

Thanks,

3条回答
祖国的老花朵
2楼-- · 2020-07-09 06:42

UIImagePickerController has a takePicture method that can be called programmatically.

查看更多
ら.Afraid
3楼-- · 2020-07-09 06:42

import this file in .h :

AVFoundation/AVCaptureInput.h
AVFoundation/AVCaptureDevice.h
AVFoundation/AVCaptureOutput.h
AVFoundation/AVMediaFormat.h    

put in .m :

- (AVCaptureDevice *)frontCamera
{
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *device in devices)
    {
        if ([device position] == AVCaptureDevicePositionFront)
        {
            return device;
        }
    }
    return nil;
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
    CGImageRef cgImage = [self imageFromSampleBuffer:sampleBuffer];
    self.theImage = [UIImage imageWithCGImage: cgImage ];
    CGImageRelease( cgImage );

    NSCalendar *sysCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    df.calendar = sysCalendar;
    [df setDateFormat:@"dd_MM_yyyy hh:mm:ss"];
    StrCapture = [NSString stringWithFormat:@"%@.jpeg",[df stringFromDate:[NSDate date]]];
    NSLog(@"StrCapture : %@",StrCapture);

    NSData *imageData = UIImageJPEGRepresentation(self.theImage,1);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:StrCapture];
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

    NSLog(@"ImagePAth : %@",fullPath);
}

- (CGImageRef) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    uint8_t baseAddress = (uint8_t )CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);
    CGContextRelease(newContext);

    CGColorSpaceRelease(colorSpace);
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    return newImage;
}
查看更多
我命由我不由天
4楼-- · 2020-07-09 06:58

You can use UIImagePickerController has a takePicture method to take picture.

For more control over the picture you can use AVFoundation header which contains avcapturestillimageoutput method to capture images. More Info

查看更多
登录 后发表回答