iphone(objective-c)相当于j2me的drawRegion方法 截取部分图片显示到指定位置
?
图片(UIImage*) img
要截取的起始坐标sx:(int) sx1 sy:(int)sy1
要截取的长度和宽度sw:(int) sw1 sh:(int) sh1
最终要显示的坐标desx:(int) desx1 desy:(int)desy1
?
-(UIImage*)objectiveDrawRegion:(UIImage*) img sx:(int) sx1 sy:(int)sy1 sw:(int) sw1 sh:(int) sh1 desx:(int) desx1 desy:(int)desy1{
[self saveImage:img name:@"objectiveDrawRegion1.png"];
?
//创建图片缓冲
void *imageDataRegion=malloc(screenWidth*screenHeight*32);
CGColorSpaceRef iColorSpaceRegion=CGColorSpaceCreateDeviceRGB();
? ? CGContextRef iDeviceRegion=CGBitmapContextCreate(imageDataRegion,screenWidth,screenHeight,8,4*screenWidth,iColorSpaceRegion,kCGImageAlphaPremultipliedLast);
?
?
//剪切区域
? ? CGRect clipRegion=CGRectMake(sx1,sy1,sw1,sh1);
? ? CGContextClipToRect(iDeviceRegion, clipRegion);
? ?
? ? CGFloat widthf=img.size.width;
? ? CGFloat heightf=img.size.height;
?
CGRect? cg=CGRectMake(0.0, 0.0, widthf, heightf);
//画底图
? ? CGContextDrawImage(iDeviceRegion,cg, img.CGImage);
//将缓冲形成图片
? ? CGImageRef ioffRegion=CGBitmapContextCreateImage(iDeviceRegion);
??
CGRect??cg1=CGRectMake(desx1,?desy1,?sw1,?sh1);
UIImage *ui=[UIImage imageWithCGImage:ioffRegion];
?
CGContextDrawImage(当前context,cg1, ui.CGImage);
//清除缓冲
? CGColorSpaceRelease(iColorSpaceRegion);
? CGContextRelease(iDeviceRegion);
? CGImageRelease(ioffRegion);
? free(imageDataRegion);
// ? iDeviceRegion=NULL;
// ? imageDataRegion=0;
return ui;
}
?
?
//这个方法更高效
/*
?实现j2me的drawRegion方法
?sx图片剪切的x位置
?sy图片剪切的y位置
?sw图片剪切的宽度
?sh图片剪切的高度
?dx剪切后的x位置
?dy剪切后的y位置
?device上下文
?*/
-(void) objectiveDrawRegion:(UIImage*) img?
sx:(int) sx1 ? ?
sy:(int) sy1?
sw:(int) sw1?
sh:(int) sh1?
dx:(int) dx1
dy:(int) dy1
device:(CGContextRef)idevicecopyOffscr
{
CGRect rect = CGRectMake(sx1, sy1, sw1, sh1);
CGImageRef imgRef = CGImageCreateWithImageInRect(img.CGImage, rect);
int dy1Trans = screenHeight - dy1 - sh1;
CGContextDrawImage(idevicecopyOffscr
? , CGRectMake(dx1, dy1Trans, sw1, sh1)
? , imgRef);
CGImageRelease(imgRef);
}
?
?
?
?