AVCam Libriary capture image saving

572 Views Asked by At

hi I am using the AVCam Liberary for automatic image capturing.I dont want to save the image in photo libriary I want to save the image in document directory .it saves the image but having problem when i load this image gives access bad.

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

and loading the image

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

when this loadedImage is assign to any UIImage

1

There are 1 best solutions below

0
On

While writing the file try -

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