iphone-CoreGraphics切割图片
来源:互联网 发布:思齐软件是什么 编辑:程序博客网 时间:2024/05/06 13:17
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
//create a context to do our clipping in
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//create a rect with the size we want to crop the image to
//the X and Y here are zero so we start at the beginning of our
//newly created context
CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);
CGContextClipToRect( currentContext, clippedRect);
//create a rect equivalent to the full size of the image
//offset the rect by the X and Y we want to start the crop
//from in order to cut off anything before them
CGRect drawRect = CGRectMake(rect.origin.x * -1,
rect.origin.y * -1,
imageToCrop.size.width,
imageToCrop.size.height);
//draw the image to our clipped context using our offset rect
CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);
//pull the image from our cropped context
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
//pop the context to get back to the default
UIGraphicsEndImageContext();
//Note: this is autoreleased
return cropped;
}
Then call the code with something like:
- (void)drawRect:(CGRect)rect
{
//draw the whole lights off image
//the on images will be drawn overtop
[lightsOffImage drawInRect:rect];
//if we don't have any lights on... no point in continuing
if( numberOfLightsOn < 1 )
return;
//figure out the dimensions of numberOfLights on bulbs
CGSize croppedSize = CGSizeMake(LIGHT_WIDTH * numberOfLightsOn, LIGHT_HEIGHT);
CGRect clippedRect = CGRectMake(0, 0, croppedSize.width, croppedSize.height);
//get the "on" bulbs by cropping the image
UIImage *cropped = [self imageByCropping:lightsOnImage toRect:clippedRect];
//create a rect to draw the newly cropped on images to
CGRect lightsOnRect = CGRectMake(LIGHT_BULB_OFFSET_X,
LIGHT_BULB_OFFSET_Y,
croppedSize.width,
croppedSize.height);
//draw the "on" lights
[cropped drawInRect:lightsOnRect];
//cropped is autoreleased so no need to worry about cleanup
}
- iphone-CoreGraphics切割图片
- 按照百分比切割圆形图片(CoreGraphics重绘)
- CoreGraphics对UIImage图片裁剪
- 切割图片
- 切割图片
- 图片切割
- 图片切割
- 切割图片
- CoreGraphics
- CoreGraphics
- CoreGraphics
- CoreGraphics
- iOS-CoreGraphics(CGAffineTransform.h)
- iphone开发 对UIImage切割
- 利用ImageMagick切割图片
- 也谈图片切割
- C#图片切割
- 图片切割软件imagemagick
- 多核并行计算时代的来临
- 写前台控件时的一个有趣的问题
- 圆角图片样式在线生成工具
- PHP调用MS SQL 存储过程
- 美工之家
- iphone-CoreGraphics切割图片
- oa试用地址
- Servlet和Filter的url匹配以及url-pattern详解
- xp_cmdshell的内容
- Gecko,Trident,Raptor
- 获取showModalDialog()的不同返回值
- 企业oa你好吗
- Apache通用日志工具commons-logging和Log4j使用
- Portal