Ios 如何裁剪具有特定位置的图像并设置为其他视图
从特定位置裁剪图像并设置为其他视图 最终图像视图Ios 如何裁剪具有特定位置的图像并设置为其他视图,ios,objective-c,Ios,Objective C,从特定位置裁剪图像并设置为其他视图 最终图像视图 self.finalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 320)]; if(rectangle_button_preesed_view) { self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(3
self.finalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 320)];
if(rectangle_button_preesed_view)
{
self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(30, 120, 260, 340)];
}
else
{
self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(30, 80, 260, 260)];
}
裁剪图像
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
NSLog(@" cropped size %f %f ",cropped.size.width,cropped.size.height);
return cropped;
}
或
除了
CoreGraphics
解决方案之外,我还建议使用CoreImage
支持图像处理过滤器。您应该查看中的CICrop
过滤器
注意:iOS 5或更高版本支持此筛选器
我目前没有一个确切的解决方案,但你可以继续
希望有帮助
@implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)cropRect {
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect);
UIImage *cropedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropedImage;
}
@implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)rect {
rect = CGRectMake(rect.origin.x*self.scale,
rect.origin.y*self.scale,
rect.size.width*self.scale,
rect.size.height*self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
UIImage *result = [UIImage imageWithCGImage:imageRef
scale:self.scale
orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}
@end
@implementation UIImage (Crop)
- (UIImage *)crop:(CGRect)cropRect {
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect);
UIImage *cropedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropedImage;
}