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;
}


你可能感兴趣的:(UI,iPhone,j2me)