2013-05-09 68 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有代码将图像转换为灰度。您可以根据需要修改它。

相关问题