1

我在我的项目中使用 Apple 的 GLCameraRipple 示例代码,我想知道是否有办法拍摄照片/视频?项目可以在https://developer.apple.com/library/ios/samplecode/GLCameraRipple/Introduction/Intro.html找到

4

1 回答 1

1

首先,确保您已包含 AssetLibrary 框架,因为我们需要它来访问照片库。假设您已经正确设置了捕获 AVCaptureSession、AVCaptureDevice 和 AVCaptureStillImageOutput (stillImage),现在您可以创建一个按钮或简单地调用以下函数来保存图像。

-(void)captureMultipleTimes
{
AVCaptureConnection *connection = [stillImage connectionWithMediaType:AVMediaTypeVideo];

typedef void(^MyBufBlock)(CMSampleBufferRef, NSError*);

MyBufBlock h = ^(CMSampleBufferRef buf, NSError *err){
    NSData *data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buf];
    [self setToSaveImage:[UIImage imageWithData:data]];

    dispatch_async(dispatch_get_main_queue(), ^{
        if(saveLabel == NULL){
            [self setSaveLabel:[[UILabel alloc] initWithFrame:CGRectMake(0,self.view.bounds.size.height/2, self.view.bounds.size.width, 50)]];
            [saveLabel setText:@"Saving.."];
            [saveLabel setTextColor:[captureBt titleColorForState:UIControlStateNormal]];
            [self.view addSubview:saveLabel];
        } else
            saveLabel.hidden = NO;

        UIImageWriteToSavedPhotosAlbum(toSaveImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    });
};
[stillImage captureStillImageAsynchronouslyFromConnection:connection completionHandler:h];
}

您还需要实现image:didFinishSavingWithError:contextInfo:方法作为保存图像的完成函数。一个例子如下:

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if(error != NULL){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Image could not be saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}
else{
    [saveLabel setHidden:YES];
}
}

一旦触发 captureMultipleTimes 函数,上述函数将在屏幕上显示“Saving..”标签。它只是意味着它当前正在将视频输入保存为图像并将其存储到照片库中。保存完成后,保存标签将从屏幕上隐藏。希望这可以帮助!

于 2013-10-30T01:27:33.283 回答