2013-05-09 5 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의 가치는 무엇입니까? 그 컴파일되지 않습니다 .. –

+0

안녕하세요, 안녕하세요, 나는 어디에서 또는 우리가 실제로 설정/픽셀 색상을 변경하는 라인을 이해하지 못했습니까? –

+1

@Vipul J이 정의되지 않은 것들을 지우거나 pixelCountForCalculatingAvgColor의 값과 avgRGBsOfPixel의 배열 값을 지울 수 있습니까? –

0

코어 그래픽이 가능합니다. link에는 이미지를 그레이 스케일로 변환하는 코드가 있습니다. 필요에 맞게 수정할 수 있습니다.

0

CGContextRef이 모두를 수행합니다. 이것으로 모든 종류의 마법을 쓸 수 있습니다.

Here are some slides 기본 사용법을 설명합니다.

관련 문제