2013-05-09 6 views

Antwort

8

Nachdem ich viel nach einem kompletten Tag gesucht hatte, bekam ich die genaue Lösung, obwohl ich viele fand, aber keiner von ihnen funktionierte. Hier ist, was ich habe, es funktioniert einwandfrei -:

// 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

Was ist Wert von pixelCountForCalculatingAvgColor und avgRGBsOfPixel? Es ist nicht compiling .. –

+0

Hey Hallo, ich habe nicht verstanden, wo oder bei welcher Zeile wir tatsächlich setzen/ändern Pixelfarbe? Bitte erklären Sie –

+1

@Vipul J können Sie diese undefinierten Dinge löschen oder was ist dieser Wert von pixelCountForCalculatingAvgColor und Array-Werte von avgRGBsOfPixel? –

0

Es ist möglich mit Core-Grafik. Die link verfügt über Code, um Ihr Bild in Graustufen zu konvertieren. Sie können es für Ihre Bedürfnisse ändern.

Verwandte Themen