2013-05-09 69 views

回答

8

經過大量的搜索後,我得到了確切的解決方案,儘管我發現了很多但他們都沒有工作。以下是我已經得到的,它完美的作品 - :

// This method creates an image by changing individual pixels of an image. Color of pixel has been taken from an array of colours('avgRGBsOfPixel') 
-(void)createTexture{ 

self.sampleImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"whitebg.jpg"]]; 
CGRect imageRect = CGRectMake(0, 0, self.sampleImage.image.size.width, self.sampleImage.image.size.height); 

UIGraphicsBeginImageContext(sampleImage.image.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

//Save current status of graphics context 
CGContextSaveGState(context); 
CGContextDrawImage(context, imageRect, sampleImage.image.CGImage); 
// And then just draw a point on it wherever you want like this: 

// CGContextFillRect(context, CGRectMake(x,y,1,1)); 
//Fix error according to @gsempe's comment 
int pixelCount =0; 
for(int i = 0 ; i<sampleImage.image.size.width ; i++){ 
    for(int j = 0 ; j<sampleImage.image.size.height ; j++){ 
     pixelCount++; 
     int index = pixelCount/pixelCountForCalculatingAvgColor; 
     //   NSLog(@"Index : %d", index); 
     if(index >= [avgRGBsOfPixel count]){ 
      index = [avgRGBsOfPixel count] -1; 
      NSLog(@"Bad Index"); 
     } 
     //   if(pixelCount >9999){ 
     //    pixelCount = 9999; 
     //    NSLog(@"Bad Index"); 
     //   } 
     UIColor *color = [avgRGBsOfPixel objectAtIndex:index]; 
     float red ,green, blue ,alpha ; 
     [color getRed:&red green:&green blue:&blue alpha:&alpha]; 
     CGContextSetRGBFillColor(context, red , green, blue , 1); 
     CGContextFillRect(context, CGRectMake(i,j,1,1)); 
    } 
} 

// Then just save it to UIImage again: 

CGContextRestoreGState(context); 
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
[self.iv setImage:img]; 

}

+6

什麼是pixelCountForCalculatingAvgColor和avgRGBsOfPixel的價值?它不編譯.. – 2014-12-04 05:58:08

+0

嗨嗨,我不明白我們實際設置/更改像素顏色的哪一行或哪一行?請解釋一下 – 2016-03-11 15:46:51

+1

@Vipul J你能清楚這個未定義的事情嗎?pixelCountForCalculatingAvgColor的值和avgRGBsOfPixel的數組值是什麼? – 2016-06-03 05:28:12

0

它可能與核心圖形。 link有代碼將圖像轉換爲灰度。您可以根據需要修改它。

相關問題