试试这个代码。
NSString *foo = @"0,0,0,1,1,1,7,7,7,7,7,0,0,1,1,1,1,100,100,100,100,0,0,0,0,0,0,1,1,1";
NSArray *colors = [foo componentsSeparatedByString:@","];
int width = 6;
int height = 5;
//create drawing context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
//draw pixels
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int index = x + y*width;
CGFloat val = [[colors objectAtIndex:index] floatValue];
val = val / 255.0f;
CGFloat components[4] = {val, val, val, 1.0f};
CGContextSetFillColor(context, components);
CGContextFillRect(context, CGRectMake(x, y, 1.0f, 1.0f));
}
}
//capture resultant image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect frame = self.imageView.frame;
frame.size = CGSizeMake(width, height);
self.imageView.frame = frame;
self.imageView.image = image;