首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVCam库捕获图像保存

AVCam库捕获图像保存
EN

Stack Overflow用户
提问于 2012-08-09 20:21:03
回答 1查看 558关注 0票数 2

嗨,我正在使用AVCam库进行自动图像捕获。我不想将图像保存在照片库中,我想将图像保存在文档目录中。.it保存图像,但当我加载此图像时出现问题,导致访问不好。

代码语言:javascript
复制
 - (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];
                                                                 }
                                                             }];
    }

并加载图像

代码语言:javascript
复制
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时

EN

回答 1

Stack Overflow用户

发布于 2014-11-21 21:10:04

在写入文件时,尝试-

代码语言:javascript
复制
[UIImagePNGRepresentation(self.imageView.image) writeToFile:pngPath atomically:YES];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11883327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档