- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ref, [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5].CGColor);
CGContextFillRect(ref, CGRectMake(0, 0, 300, 400));
CGContextSetStrokeColorWithColor(ref, [UIColor blueColor].CGColor);
CGContextAddEllipseInRect(ref, CGRectMake(0, 30, 100, 100));
CGContextMoveToPoint(ref, 50, 80);
CGContextAddLineToPoint(ref, 200, 250);
CGContextStrokePath(ref);
CGContextRelease(ref);//context需要自己释放,CGImageRef也需自己释放
}
- (CGContextRef)myCGContextRef
{
// CGContextRef ref = [self makeBitmapContextWithPixelWidth:400 height:300];
UIGraphicsBeginImageContext(CGSizeMake(400, 300));
//UIGraphicsBeginImageContextWithOptions(CGSizeMake(400, 300), NO, 1.0);
CGContextRef ref = UIGraphicsGetCurrentContext();
return ref;
}
- (CGContextRef)makeBitmapContextWithPixelWidth:(NSInteger)width height:(NSInteger)height
{
NSInteger bitPerComponent = 8;
NSInteger bytesPerRow = width * 4 * bitPerComponent;
CGContextRef bitmapContextRef = CGBitmapContextCreate(NULL, width, height, bitPerComponent, bytesPerRow, CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), kCGImageAlphaPremultipliedLast);
//第二种方式
/*
CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage], rect);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
CGContextRef bitmap = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
*/
return bitmapContextRef;
}
UIGraphicsBeginImageContex或UIGraphicsBeginImageContextWithOptions获取context或CGBitmapContextCreate,不同之处在于前两者的context坐标系原点在左下角,后者在左上角。drawRect中的context是UIView自动生成返回的,坐标系原点在左上角
对以下2情况,CGContext坐标系在左上角:
1) In iOS, a drawing context returned by an UIView.(如drawRect中context)
2) In iOS, a drawing context created by calling the UIGraphicsBeginImageContextWithOptions function.
context坐标系在左上角
context坐标系在左下角
drawRect函数可直接将图画在view上,还有一种方式便是自己画图,获取context中图片,然后将图片传给imageView或其他在view上展示出来
- (UIImage *)drawImage
{
CGContextRef ref = [self myCGContextRef];
//用现有的image直接draw
UIImage *originImage = [UIImage imageNamed:@"D.jpg"];
UIImage *newImage = [UIImage imageWithCGImage:originImage.CGImage scale:1.0 orientation:UIImageOrientationRight];
CGContextDrawImage(ref, CGRectMake(0, 0, 300, 400), newImage.CGImage);
// [newImage drawInRect:CGRectMake(0, 0, 400, 300)];
//自己画
CGContextSetFillColorWithColor(ref, [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5].CGColor);
CGContextFillRect(ref, CGRectMake(0, 0, 300, 400));
CGContextSetStrokeColorWithColor(ref, [UIColor blueColor].CGColor);
CGContextAddEllipseInRect(ref, CGRectMake(0, 30, 100, 100));
CGContextMoveToPoint(ref, 50, 100);
CGContextAddLineToPoint(ref, 200, 250);
CGContextStrokePath(ref);
//最后获取context中画的的image,然后传给imageView展示出来
CGImageRef imageRef = CGBitmapContextCreateImage(ref);
UIImage *image = [UIImage imageWithCGImage:imageRef];
//需要自己释放
CGImageRelease(imageRef);
CGContextRelease(ref);
return image;
}
需要注意的是,用现有的image直接draw的方式,系统会将image上下翻转(坐标系不翻转)。故对于context坐标系在左下角的情况,图片的方向和我们想要的方向是一致的(UIKit坐标系),对于context坐标系在左上角的情况,图片就是上下翻转的,需要我们自己处理,或者用drawInRect:代替CGContextDrawImage,前者的坐标系是左上角,且要用UIGraphicsBeginImageContex造context,CGBitmapContextCreate造的画不出来。代码一般如下:
//一般此函数是将image方向正为UIImageOrientationUp
- (UIImage *)contextImage:(UIImage *)image
{
UIGraphicsBeginImageContext(CGSizeMake(_image.size.width, _image.size.height));
//UIGraphicsBeginImageContextWithOptions(CGSizeMake(_image.size.width, _image.size.height), NO, 1.0);
[_image drawInRect:CGRectMake(0, 0, _image.size.width, _image.size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_image = scaledImage;
return scaledImage;
}
采用CGContextDrawImage,context坐标系在左下角,因image自己会上下翻转,所以画出来的图是正的
采用CGContextDrawImage,context坐标系在左上角,因image自己会上下翻转,所以画出来的图是上下翻转的
采用drawInRect:,且UIGraphicsBeginImageContex造context,画出来的是正的