2013-04-18 75 views
1

我使用CGDataProviderCopyData從圖像中檢索數據,但與圖像文件大小相比,此函數返回的字節非常大。這是我的代碼。從圖像中檢索RGBA值

UIImage *image = self.imageView.image; 
CGImageRef cgimage = image.CGImage; 
CGDataProviderRef provider = CGImageGetDataProvider(cgimage); 
NSData* data = (__bridge_transfer NSData*)CGDataProviderCopyData(provider); 

是否有任何其他方法來讀取像素數據並從圖像中獲取rgba值。

+0

原始RGBA應該是比壓縮大得多圖片。 – danh

回答

0

你在做什麼是正確的。磁盤上的映像文件是壓縮格式。將數據加載到UIImage中時,圖像是未壓縮的,需要「寬x高x 4」字節。 「4」假定RGBA。實際上,由於每行的字節數通常是16字節的倍數,因此數據可能會更大一些。

有關從圖像中加載字節的一件事。不要以爲它是RGBA。根據圖像,它可能是其他格式。使用適當的函數來確定顏色模型,每像素字節數和每行字節數。

1

你好,這是方式找到的值...

CGImageRef imgSource=self.duplicateImage.image.CGImage; 
    CFDataRef m_DataRef1 = CGDataProviderCopyData(CGImageGetDataProvider(imgSource)); 
    UInt8 *dataOriginal=(UInt8 *)CFDataGetBytePtr(m_DataRef1); 
    double lengthSource=CFDataGetLength(m_DataRef1); 
    NSLog(@"length::%f",lengthSource); 

下面一個是修改值的例子...

-(UIImage*)customBlackFilterOriginal 
{ 
    CGImageRef imgSource=self.duplicateImage.image.CGImage; 
    CFDataRef m_DataRef1 = CGDataProviderCopyData(CGImageGetDataProvider(imgSource)); 
    UInt8 *dataOriginal=(UInt8 *)CFDataGetBytePtr(m_DataRef1); 
    double lengthSource=CFDataGetLength(m_DataRef1); 
    NSLog(@"length::%f",lengthSource); 
    int redPixel; 
    int greenPixel; 
    int bluePixel; 

    for(int index=0;index<lengthSource;index+=4) 
    { 

     dataOriginal[index]=dataOriginal[index]; 
     dataOriginal[index+1]= 101; 
     dataOriginal[index+2]= 63; 
     dataOriginal[index+3]=43;  

    } 

    NSUInteger width =CGImageGetWidth(imgSource); 
    size_t height=CGImageGetHeight(imgSource); 
    size_t bitsPerComponent=CGImageGetBitsPerComponent(imgSource); 
    size_t bitsPerPixel=CGImageGetBitsPerPixel(imgSource); 
    size_t bytesPerRow=CGImageGetBytesPerRow(imgSource); 

    NSLog(@"the w:%u H:%lu",width,height); 

    CGColorSpaceRef colorspace=CGImageGetColorSpace(imgSource); 
    CGBitmapInfo bitmapInfo=CGImageGetBitmapInfo(imgSource); 
    CFDataRef newData=CFDataCreate(NULL,dataOriginal,lengthSource); 
    CGDataProviderRef provider=CGDataProviderCreateWithCFData(newData); 
    CGImageRef newImg=CGImageCreate(width,height,bitsPerComponent,bitsPerPixel,bytesPerRow,colorspace,bitmapInfo,provider,NULL,true,kCGRenderingIntentDefault); 

    return [UIImage imageWithCGImage:newImg]; 

} 
+0

供您參考:http://stackoverflow.com/questions/10516691/how-to-detect-the-skin-color-of-face-from-the-source-image-in-ios-5 – Spynet