我有一個形象,我是從AVFoundation抓:保存CIImage,通過轉換爲CGImage,拋出一個錯誤
[stillImage captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
NSLog(@"image capture");
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
我那麼首先轉換爲CIImage剪裁此圖片:
beginImage = [CIImage imageWithCVPixelBuffer:CMSampleBufferGetImageBuffer(imageSampleBuffer) options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], kCIImageColorSpace, nil]];
//first we crop to a square
crop = [CIFilter filterWithName:@"CICrop"];
[crop setValue:[CIVector vectorWithX:0 Y:0 Z:70 W:70] forKey:@"inputRectangle"];
[crop setValue:beginImage forKey:@"inputImage"];
croppedColourImage = [crop valueForKey:@"outputImage"];
我再嘗試轉換這回CGImage,這樣我可以節省出來:
CGImageRef cropColourImage = [context createCGImage:croppedColourImage fromRect:[croppedColourImage extent]];
UIImage *cropSaveImage = [UIImage imageWithCGImage:cropColourImage];
//saving of the images
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[cropSaveImage CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(@"ERROR in image save :%@", error);
} else
{
NSLog(@"SUCCESS in image save");
}
}];
但是,這將引發錯誤:
ERROR in image save :Error Domain=ALAssetsLibraryErrorDomain Code=-3304 "Failed to encode image for saved photos." UserInfo=0x19fbd0 {NSUnderlyingError=0x18efa0 "Failed to encode image for saved photos.", NSLocalizedDescription=Failed to encode image for saved photos.}
我讀過其他地方,這可能發生,如果圖像是爲0x0像素,以及從CIImage到CGImage轉換可能會導致問題。有任何想法嗎?
謝謝。
我相信這是因爲我試圖保存的圖像沒有尺寸,寬度和高度顯示爲空。 「2012-05-06 14:39:22.886 zApp [5108:707]正在保存的圖像的寬度爲:(null) 2012-05-06 14:39:22.890 zApp [5108:707]正在保存的圖像的高度爲:(null)「 – mrEmpty
我在我的NSLog代碼中有一個錯誤,我可以確認我想要保存的圖像是0x0像素。所以我認爲是問題所在,但我不明白會發生什麼。 – mrEmpty