Olen modannut AVCam-nimistä sample codea omaan käyttööni. Alkuperäisessä on tällainen metodi, joka ottaa kuvan ja tallentaa sen Photos-albumiin. Tämä toimii aivan mainiosti.
Koodi: Valitse kaikki
- (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];
[library writeImageToSavedPhotosAlbum:[image CGImage]
orientation:(ALAssetOrientation)[image imageOrientation]
completionBlock:completionBlock];
[image release];
[library release];
}
else
completionBlock(nil, error);
if ([[self delegate] respondsToSelector:@selector(captureManagerStillImageCaptured:)]) {
[[self delegate] captureManagerStillImageCaptured:self];
}
}];
}
Seuraavaksi pitäisi kai tehdä autorelease ja palauttaa kuva kutsujalle, mutta tuo if-lause aiheuttaa hämmennystä. Jos laitan rivin "return image;" aivan metodin loppuun, tulee tietysti virheilmoitus, koska image luodaan if-lauseen sisällä, eikä sen olemassaolo metodin loppupuolella ylipäätään ole varmaa.
Jos laitan tuonne if-lauseen sisään return [UIImage imageWithData:imageData]; ja loppuun return nil; ...tulee tällainen virheilmoitus: "error: incompatible block pointer types initializing 'struct UIImage * (^)(struct opaqueCMSampleBuffer *, struct NSError *)', expected 'void (^)(struct opaqueCMSampleBuffer *, struct NSError *)'"
Mitenhän tämä onnistuisi?