2012-05-04 60 views
0

我有一個形象,我是從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轉換可能會導致問題。有任何想法嗎?

謝謝。

+0

我相信這是因爲我試圖保存的圖像沒有尺寸,寬度和高度顯示爲空。 「2012-05-06 14:39:22.886 zApp [5108:707]正在保存的圖像的寬度爲:(null) 2012-05-06 14:39:22.890 zApp [5108:707]正在保存的圖像的高度爲:(null)「 – mrEmpty

+0

我在我的NSLog代碼中有一個錯誤,我可以確認我想要保存的圖像是0x0像素。所以我認爲是問題所在,但我不明白會發生什麼。 – mrEmpty

回答

1

我得到了它的工作(戰地3上的20分鐘讓我有時間思考 - 這讓我想起任何PS3 BF3玩家想要給我發送消息)。

我所做的是取代我的所有太妃糖代碼:

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
UIImage *image = [[UIImage alloc] initWithData:imageData]; 
NSLog(@"*image size is: %@", NSStringFromCGSize(image.size)); 

//test creating a new ciimage 
CIImage *ciImage = [[CIImage alloc] initWithCGImage:[image CGImage]]; 
NSLog(@"ciImage extent: %@", NSStringFromCGRect([ciImage extent])); 

這給了我一個可用的CIImage。那麼我可以過濾它的樂趣,與保存前:

CIImage *croppedColourImage = [crop outputImage]; 
    //convert it to a cgimage for saving 
    CGImageRef cropColourImage = [context createCGImage:croppedColourImage fromRect:[croppedColourImage extent]]; 
    ////NSLog(@"beginImage width is: %@", NSStringFromCGRect([beginImage extent])); 
    ////NSLog(@"croppedColourImage extent: %@", NSStringFromCGRect([croppedColourImage extent])); 
    ////NSLog(@"cropColourImage size: %zu", CGImageGetWidth(cropColourImage)); 
    //UIImage *cropSaveImage = [UIImage imageWithCGImage:cropColourImage]; 


    //saving of the images 
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

    [library writeImageToSavedPhotosAlbum:cropColourImage metadata:[croppedColourImage properties] completionBlock:^(NSURL *assetURL, NSError *error){ 
    if (error) { 
     NSLog(@"ERROR in image save: %@", error); 
     //NSLog(@"Image being saved has width of: %zu", CGImageGetWidth(cropColourImage)); 
     // NSLog(@"Image being saved has height of: %zu", CGImageGetHeight(cropColourImage)); 
    } else 
    { 
     NSLog(@"SUCCESS in image save"); 
     CGImageRelease(cropColourImage); 
    } 

    }]; 

請忽略廢話很多註釋行,這些都是我的測試。他們現在可以擺脫。

我很高興周圍的花園接近慶祝運行。

+0

爲什麼cropColourImage沒有發佈出錯? –