-(UIImage *)imageFromColor:(UIColor*)color
{
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
如何把一个图片设置成椭圆形状
UIImage * srcImg =[UIImage imageNamed:@"videoHoldbg.png"];
CGFloat width = srcImg.size.width;
CGFloat height = srcImg.size.height;
//开始绘制图片
UIGraphicsBeginImageContext(srcImg.size);
CGContextRef gc = UIGraphicsGetCurrentContext();
绘制Clip区域
CGContextAddEllipseInRect(gc, CGRectMake(0, 0,100, 75)); //椭圆
CGContextClosePath(gc);
CGContextClip(gc);
//坐标系转换
//因为CGContextDrawImage会使用Quartz内的以左下角为(0,0)的坐标系
CGContextTranslateCTM(gc, 0, height);
CGContextScaleCTM(gc, 1, -1);
CGContextDrawImage(gc, CGRectMake(0, 0, width, height), [srcImg CGImage]);
//结束绘画
UIImage *destImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * view =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[view setImage:destImg];
[self.view addSubview:view];
Draw another image on a UIImage?http://stackoverflow.com/questions/6656745/draw-another-image-on-a-uiimage
CGFloat width, height;
UIImage *inputImage; // input image to be composited over new image as example
// create a new bitmap image context at the device resolution (retina/non-retina)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), YES, 0.0);
// get context
CGContextRef context = UIGraphicsGetCurrentContext();
// push context to make it current
// (need to do this manually because we are not drawing in a UIView)
UIGraphicsPushContext(context);
// drawing code comes here- look at CGContext reference
// for available operations
// this example draws the inputImage into the context
[inputImage drawInRect:CGRectMake(0, 0, width, height)];
// pop context
UIGraphicsPopContext();
// get a UIImage from the image context- enjoy!!!
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
// clean up drawing environment
UIGraphicsEndImageContext();
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];
UIImage *myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath];
// Store the image into a property of type UIImage *
// for use later in the class's drawRect: method.
self.anImage = myImageObj;
- (void)drawRect:(CGRect)rect
{
...
// Draw the image.
[self.anImage drawAtPoint:CGPointMake(10, 10)];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
if (image != nil && image.size.width != kAppIconHeight && image.size.height != kAppIconHeight) {
CGRect imageRect = CGRectMake(0.0, 0.0, kAppIconHeight, kAppIconHeight);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, [UIScreen mainScreen].scale);
[image drawInRect:imageRect];
self.appRecord.appIcon = UIGraphicsGetImageFromCurrentImageContext(); // UIImage returned.
UIGraphicsEndImageContext();
} else {
self.appRecord.appIcon = image;
}
self.activeDownload = nil;
[image release];
self.imageConnection = nil;
[delegate appImageDidLoad:self.indexPathInTableView];
}
/ Other code precedes...
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
pdfScale = self.frame.size.width/pageRect.size.width;
pageRect.size = CGSizeMake(pageRect.size.width * pdfScale, pageRect.size.height * pdfScale);
UIGraphicsBeginImageContextWithOptions(pageRect.size, YES, pdfScale);
CGContextRef context = UIGraphicsGetCurrentContext();
// First fill the background with white.
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,pageRect);
CGContextSaveGState(context);
// Flip the context so that the PDF page is rendered right side up
CGContextTranslateCTM(context, 0.0, pageRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Scale the context so that the PDF page is rendered at the
// correct size for the zoom level.
CGContextScaleCTM(context, pdfScale,pdfScale);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
// Other code follows...