-(void)drawImageRect:(CGRect)rect
{
UIImage *image=[UIImage imageNamed:@"iphone.jpg"];
//原图片
CGImageRef imageRef=image.CGImage;
CGImageRef subImageRef=CGImageCreateWithImageInRect(imageRef, rect);
//原图中抠出来部分图
UIImage *subImage=[UIImage imageWithCGImage:subImageRef];
//新图
_imageView.image=subImage;
_imageView.frame=rect;
[UIView beginAnimations:nil context:nil];
//改变frame,改变过程中添加动画
[UIView setAnimationDuration:0.5];
_imageView.frame=CGRectMake(0, 20, rect.size.width, rect.size.height);
[UIView commitAnimations];
}
UIImage *image=[UIImage imageNamed:@"iphone.jpg"];
//原图片
CGImageRef imageRef=image.CGImage;
CGImageRef subImageRef=CGImageCreateWithImageInRect(imageRef, rect);
//原图中抠出来部分图
UIImage *subImage=[UIImage imageWithCGImage:subImageRef];
//新图
_imageView.image=subImage;
_imageView.frame=rect;
这几行代码是关键,将UIImage类型的image转化成CGImageRef类型的imageRef,然后根据imageRef和划出的区域取出画出的部分,再将这部分重新转化成UIImage类型。最后给UIImageView类型的_imageView显示出来。