AVCam Library捕获图像保存

时间:2012-08-09 12:21:03

标签: ios avcam

嗨我正在使用AVCam Liberary进行自动图像捕捉。我不想这样做 将图像保存在照片库中我想将图像保存在文档目录中。保存图像但是当我有问题时 加载此图像会导致访问不良。

 - (void) captureStillImage
    {
        AVCaptureConnection *stillImageConnection = [AVCamUtilities connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
        if ([stillImageConnection isVideoOrientationSupported])
            [stillImageConnection setVideoOrientation:orientation];

        [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:stillImageConnection
                                                             completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

                                                                 ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {
                                                                     if (error) {
                                                                         if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) {
                                                                             [[self delegate] captureManager:self didFailWithError:error];
                                                                             }
                                                                     }
                                                                 };

                                                                 if (imageDataSampleBuffer != NULL) {
                                                                     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                                                                     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

                                                                     UIImage *image = [[UIImage alloc] initWithData:imageData];
                                                                     NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
                                                                     NSString *docDirectory = [sysPaths objectAtIndex:0];
                                                                     NSString *filePath = [NSString stringWithFormat:@"%@/Image.jpg", docDirectory];

                                                                     NSData *imageDataToSave = [NSData dataWithData:UIImagePNGRepresentation(image)];
                                                                     [imageDataToSave writeToFile:filePath atomically:YES];
                                                                     //[self saveImage:image];

                                                                                           completionBlock:completionBlock];

                                                                     [image release];

                                                                     [library release];
                                                                 }
                                                                 else
                                                                     completionBlock(nil, error);

                                                                 if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) {
                                                                     [[self delegate] captureManagerStillImageCaptured:self];
                                                                 }
                                                             }];
    }

并加载图片

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/Image.jpg", docDirectory];
UIImage* loadedImage = [UIImage imageWithContentsOfFile:filePath];
[ImageView setImage:loadedImage];

将此loadedImage分配给任何UIImage

1 个答案:

答案 0 :(得分:0)

在编写文件时尝试 -

[UIImagePNGRepresentation(self.imageView.image) writeToFile:pngPath atomically:YES];