在放置在UIImageView上的矩形的框架中创build图像

如何用UIView中的指定框架创build一个新的图像,该视图也包含图像视图和button。我还需要将指定框架中的图像作为新图像。 我试过这个,但不正确。

我需要从UIView作物,并作为一个新的UIImage。

你需要在你的项目中导入这个框架。

#import <QuartzCore/QuartzCore.h> 

并使用下面的代码

 [imageView.layer setBorderColor: [[UIColor blackColor] CGColor]]; [imageView.layer setBorderWidth: 2.0]; // set border width as per your requirement. 

编辑:

使用你需要的任何一个。

获取裁剪图像:

 UIImage *croppedImg = nil; CGRect cropRect = CGRectMake(AS YOu Need); croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect]; 

使用下面的方法返回UIImage如你想要的图像大小

 - (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect { //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15); CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); UIImage *cropped = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); return cropped; } 

在这里你可以得到通过上面的方法返回的Croped图像;

或resize

还可以使用以下方法对图像进行特定的高度宽度调整UIImage大小

 + (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height { CGSize newSize = CGSizeMake(width, height); float widthRatio = newSize.width/image.size.width; float heightRatio = newSize.height/image.size.height; if(widthRatio > heightRatio) { newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio); } else { newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio); } UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

这个方法返回具有你想要的特定大小的NewImage