1,填充背景色:
[[UIColorpurpleColor]setFill];
UIRectFill(rect);//填充背景色:
2,设置边框颜色
[[UIColorredColor]setFill];
CGRect frame =CGRectMake(20,30,100, 300);
UIRectFrame(frame);//设置边框颜色
3,给图片上面增加水印或文字
重写UIView的- (void)drawRect:(CGRect)rect方法,然后将图形绘制到view上面,然后再增加文字或其他图层,如:
- (void)drawRect:(CGRect)rect
{
NSString* imagePath = [[NSBundlemainBundle]pathForResource:@"dog"ofType:@"png"];//rabbit
UIImage* myImageObj = [[UIImagealloc]initWithContentsOfFile:imagePath];
[myImageObjdrawInRect:CGRectMake(0,40,200, 300)];
[myImageObjdrawAsPatternInRect:CGRectMake(0,0,320, 400)];
NSString *s =@"我的小狗";
UIFont *font = [UIFontsystemFontOfSize:34];
NSDictionary *attr =@{NSFontAttributeName:font};
[drawAtPoint:CGPointMake(100,300)withAttributes:attr];
}
*,绘制曲线:
- (void)drawRect:(CGRect)rect
{
CGContextRef cgContext =UIGraphicsGetCurrentContext();//坐标
CGContextMoveToPoint(cgContext, 333, 0);
/*绘制曲线
CGContextAddCurveToPoint(cgContext, 333, 0, 332, 26, 330, 26);
CGContextAddCurveToPoint(cgContext, 330, 26, 299, 20, 299, 17);
CGContextAddLineToPoint(cgContext, 296, 17);
CGContextAddCurveToPoint(cgContext, 296, 17, 296, 19, 291, 19);
CGContextAddLineToPoint(cgContext, 250, 19);
CGContextAddCurveToPoint(cgContext, 250, 19, 241, 24, 238, 19);
CGContextAddCurveToPoint(cgContext, 236, 20, 234, 24, 227, 24);
CGContextAddCurveToPoint(cgContext, 220, 24, 217, 19, 216, 19);
CGContextAddCurveToPoint(cgContext, 214, 20, 211, 22, 207, 20);
CGContextAddCurveToPoint(cgContext, 207, 20, 187, 20, 182, 21);
CGContextAddLineToPoint(cgContext, 100, 45);
CGContextAddLineToPoint(cgContext, 97, 46);
CGContextAddCurveToPoint(cgContext, 97, 46, 86, 71, 64, 72);
CGContextAddCurveToPoint(cgContext, 42, 74, 26, 56, 23, 48);
CGContextAddLineToPoint(cgContext, 9, 47);
CGContextAddCurveToPoint(cgContext, 9, 47, 0, 31, 0, 0);
CGContextStrokePath(cgContext);
}
*,图片的缩放,平移,旋转示例
- (void)drawRect:(CGRect)rect
{
NSString *path = [[NSBundlemainBundle] pathForResource:@"cat"ofType:@"png"];
UIImage *img = [UIImageimageWithContentsOfFile:path];
CGImageRef image = img.CGImage;
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM (context,150, 200);//平移
CGContextScaleCTM (context, 1.0, .75);//缩放
CGContextRotateCTM (context, radians(275.));//旋转
CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, touchRect, image);
CGContextRestoreGState(context);
}
还可以得用图层来平称图片:如:
CGAffineTransform moveTransform = CGAffineTransformMakeTranslation(180, 400);
[myImageView.layer setAffineTransform:moveTransform];//平移