2

嗨,我正在使用 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

4

1 回答 1

0

在写入文件时尝试 -

[UIImagePNGRepresentation(self.imageView.image) writeToFile:pngPath atomically:YES];
于 2014-11-21T13:10:03.550 回答