1. 程式人生 > >UIImage 裁剪圖片和等比列縮放圖片

UIImage 裁剪圖片和等比列縮放圖片

轉自 http://www.tedz.me/ios/uiimage-crop-resize-image

@interfaceUIImage(UIImageScale) -(UIImage*)getSubImage:(CGRect)rect; -(UIImage*)scaleToSize:(CGSize)size; @end @implementation UIImage(UIImageScale) //擷取部分影象 -(UIImage*)getSubImage:(CGRect)rect { CGImageRef subImageRef=CGImageCreateWithImageInRect(self
.CGImage,rect); CGRect smallBounds=CGRectMake(0,0, CGImageGetWidth(subImageRef),CGImageGetHeight(subImageRef)); UIGraphicsBeginImageContext(smallBounds.size); CGContextRef context=UIGraphicsGetCurrentContext(); CGContextDrawImage(context,smallBounds,subImageRef); UIImage*smallImage=[UIImage imageWithCGImage
:subImageRef]; UIGraphicsEndImageContext(); returnsmallImage; } //等比例縮放 -(UIImage*)scaleToSize:(CGSize)size { CGFloat width=CGImageGetWidth(self.CGImage); CGFloat height=CGImageGetHeight(self.CGImage); floatverticalRadio=size.height*1.0/height; floathorizontalRadio=size.width*1.0/width; floatradio=1; if
(verticalRadio>1&&horizontalRadio>1) { radio=verticalRadio>horizontalRadio?horizontalRadio:verticalRadio; } else { radio=verticalRadio<horizontalRadio?verticalRadio:horizontalRadio; } width=width*radio; height=height*radio; intxPos=(size.width-width)/2; intyPos=(size.height-height)/2; // 建立一個bitmap的context // 並把它設定成為當前正在使用的context UIGraphicsBeginImageContext(size); // 繪製改變大小的圖片 [selfdrawInRect:CGRectMake(xPos,yPos, width,height)]; // 從當前context中建立一個改變大小後的圖片 UIImage*scaledImage=UIGraphicsGetImageFromCurrentImageContext(); // 使當前的context出堆疊 UIGraphicsEndImageContext(); // 返回新的改變大小後的圖片 returnscaledImage; } @end 然後在下面方法裡面呼叫就可以了! -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UIImage *image=[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img21.mtime.cn/mg/2011/09/24/112524.53149978.jpg"]] ]; // 裁剪圖片 //image = [image getSubImage:CGRectMake(10, 10, 70, 80)]; //等比列縮放 image=[image scaleToSize:CGSizeMake(200,300)]; UIImageView *imageView=[[UIImageView alloc] initWithImage:image]; [self.window addSubview:imageView]; NSLog(@"image.size:%@",NSStringFromCGSize(CGSizeMake(imageView.image.size.width,imageView.image.size.height)));//列印獲取的網路圖片的寬度和高度 [self.window makeKeyAndVisible]; returnYES; } //--------------擷取部分圖片到指定位置------------------------- 圖片(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{ [selfsaveImage:img name:@"objectiveDrawRegion1.png"]; //建立圖片緩衝 void*imageDataRegion=malloc(screenWidth*screenHeight*32); CGColorSpaceRef iColorSpaceRegion=CGColorSpaceCreateDeviceRGB(); CGContextRefiDeviceRegion=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; returnui; }