4

I've been searching this forum up and down but I couldn't find what I really need. I want to get raw image data from the camera. Up till now I tried to get the data out of the imageDataSampleBuffer from that method captureStillImageAsynchronouslyFromConnection:completionHandler: and to write it to an NSData object, but that didn't work. Maybe I'm on the wrong track or maybe I'm just doing it wrong. What I don't want is for the image to be compressed in any way.

The easy way is to use jpegStillImageNSDataRepresentation: from AVCaptureStillImageOutput, but like I said I don't want it to be compressed.

Thanks!

Dunja Lalic
  • 752
  • 13
  • 26
thomketler
  • 418
  • 4
  • 9

1 Answers1

5

This is how i do it:

1: I first open the camera using:

- (void)openCamera
{
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {        
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              (NSString *) kUTTypeMovie, nil];
        imagePicker.allowsEditing = NO;

        [self presentModalViewController:imagePicker animated:YES];
    }
    else {
        lblError.text = NSLocalizedStringFromTable(@"noCameraFound", @"Errors", @"");  
    }
}

When the picture is taken this method gets called:

 -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    // Save and get the path of the image
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    if([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
    {
        // Save the image
        image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
            if(!error) {
               //Save path and location to database
               NSString *pathLocation = [[NSString alloc] initWithFormat:@"%@", assetURL];
            } else {
                NSLog(@"CameraViewController: Error on saving image : %@ {imagePickerController}", error);
            }
        }];

    }
    [imagePicker dismissModalViewControllerAnimated:YES];
}

Then with that path i get the picture from the library in FULL resolution (using the "1"):

 -(void)preparePicture: (NSString *) filePathPicture{
    ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myasset)
    {
        if(myasset != nil){
            ALAssetRepresentation *assetRep = [myasset defaultRepresentation];
            CGImageRef imageRef = [assetRep fullResolutionImage];
            if (imageRef) {
                NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 1);
            }
        }else {
            //error
        }
     };

    ALAssetsLibraryAccessFailureBlock failureBlock  = ^(NSError *error)
    {      
         NSString *errorString = [NSString stringWithFormat:@"can't get image, %@",[error localizedDescription]];
        NSLog(@"%@", errorString);
    };

     if(filePathPicture && [filePathPicture length])
     {
         NSURL *assetUrl = [NSURL URLWithString:filePathPicture];
         ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:assetUrl 
                   resultBlock:resultBlock
                  failureBlock:failureBlock];
    }
}

Hope this helps you a bit further :-).

Zillan
  • 720
  • 7
  • 15
  • Is there no compression from UIImageJPEGRepresentation when I'm using the value 1 ? That would be really awesome – thomketler May 22 '12 at 12:22
  • In my opinion, not. 1 is the highest value and will not result in a compression. – Zillan May 24 '12 at 09:09
  • 1
    If you read Apple documentation for 'UIImageJPEGRepresentation' *The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).* . Yes, it IS compressed. – Vinzzz Jan 21 '13 at 13:21