4

このフォーラムを上下に検索してきましたが、本当に必要なものが見つかりませんでした。カメラから生の画像データを取得したい。今まで、そのメソッドから imageDataSampleBuffer からデータを取り出して オブジェクトcaptureStillImageAsynchronouslyFromConnection:completionHandler: に書き込もうとしましたが、うまくいきNSDataませんでした。たぶん、私は間違った道を進んでいるのかもしれませんし、単にやり方が間違っているだけかもしれません。私が望んでいないのは、画像が何らかの方法で圧縮されることです。

簡単な方法はjpegStillImageNSDataRepresentation: fromを使用するAVCaptureStillImageOutputことですが、圧縮したくないと言ったように。

ありがとう!

4

1 に答える 1

5

これは私がそれを行う方法です:

1: 最初に次を使用してカメラを開きます。

- (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", @"");  
    }
}

写真が撮影されると、次のメソッドが呼び出されます。

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

次に、そのパスを使用して、ライブラリからフル解像度で画像を取得します(「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];
    }
}

これがもう少し役立つことを願っています:-)。

于 2012-05-22T09:10:51.037 に答える