Iphone 模糊图像的特定部分(矩形、圆形)?
我想模糊图像矩形或圆形。在谷歌搜索之后,我发现很容易模糊整个图像,但很难模糊图像的特定部分(矩形、圆形)。那么这怎么可能呢 提前感谢 只需将Iphone 模糊图像的特定部分(矩形、圆形)?,iphone,objective-c,ios,Iphone,Objective C,Ios,我想模糊图像矩形或圆形。在谷歌搜索之后,我发现很容易模糊整个图像,但很难模糊图像的特定部分(矩形、圆形)。那么这怎么可能呢 提前感谢 只需将UIImageView属性名设置为“imageView”,并在实现文件中添加以下四个具有相同顺序的方法。 另外,将ImageView模式设置为“重画”。为给定的模糊效果添加UIImage类别,或使用任何自定义类,它都适用于您 方法1-裁剪图像 #pragma mark - Cropping the Image - (UIImage *)croppIngi
UIImageView
属性名设置为“imageView”,并在实现文件中添加以下四个具有相同顺序的方法。
另外,将ImageView模式设置为“重画”。为给定的模糊效果添加UIImage类别,或使用任何自定义类,它都适用于您
方法1-裁剪图像
#pragma mark - Cropping the Image
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
方法2-合并两个图像
#pragma mark - Marge two Images
- (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2 andRect:(CGRect)cropRect{
CGSize size = CGSizeMake(imageView.image.size.width, imageView.image.size.height);
UIGraphicsBeginImageContext(size);
CGPoint pointImg1 = CGPointMake(0,0);
[img drawAtPoint:pointImg1];
CGPoint pointImg2 = cropRect.origin;
[img2 drawAtPoint: pointImg2];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
方法3-对图像进行RoundRect
#pragma mark - RoundRect the Image
- (UIImage *)roundedRectImageFromImage:(UIImage *)image withRadious:(CGFloat)radious {
if(radious == 0.0f)
return image;
if( image != nil) {
CGFloat imageWidth = image.size.width;
CGFloat imageHeight = image.size.height;
CGRect rect = CGRectMake(0.0f, 0.0f, imageWidth, imageHeight);
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
const CGFloat scale = window.screen.scale;
UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSaveGState(context);
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM (context, radious, radious);
CGFloat rectWidth = CGRectGetWidth (rect)/radious;
CGFloat rectHeight = CGRectGetHeight (rect)/radious;
CGContextMoveToPoint(context, rectWidth, rectHeight/2.0f);
CGContextAddArcToPoint(context, rectWidth, rectHeight, rectWidth/2.0f, rectHeight, radious);
CGContextAddArcToPoint(context, 0.0f, rectHeight, 0.0f, rectHeight/2.0f, radious);
CGContextAddArcToPoint(context, 0.0f, 0.0f, rectWidth/2.0f, 0.0f, radious);
CGContextAddArcToPoint(context, rectWidth, 0.0f, rectWidth, rectHeight/2.0f, radious);
CGContextRestoreGState(context);
CGContextClosePath(context);
CGContextClip(context);
[image drawInRect:CGRectMake(0.0f, 0.0f, imageWidth, imageHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
return nil;
}
方法4-触摸移动
#pragma mark - Touch Methods
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UIImage *croppedImg = nil;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.imageView];
double ratioW=imageView.image.size.width/imageView.frame.size.width ;
double ratioH=imageView.image.size.height/imageView.frame.size.height;
currentPoint.x *= ratioW;
currentPoint.y *= ratioH;
double circleSizeW = 30 * ratioW;
double circleSizeH = 30 * ratioH;
currentPoint.x = (currentPoint.x - circleSizeW/2<0)? 0 : currentPoint.x - circleSizeW/2;
currentPoint.y = (currentPoint.y - circleSizeH/2<0)? 0 : currentPoint.y - circleSizeH/2;
CGRect cropRect = CGRectMake(currentPoint.x , currentPoint.y, circleSizeW, circleSizeH);
NSLog(@"x %0.0f, y %0.0f, width %0.0f, height %0.0f", cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height );
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];
// Blur Effect
croppedImg = [croppedImg imageWithGaussianBlur9];
// Contrast Effect
// croppedImg = [croppedImg imageWithContrast:50];
croppedImg = [self roundedRectImageFromImage:croppedImg withRadious:4];
imageView.image = [self addImageToImage:imageView.image withImage2:croppedImg andRect:cropRect];
}
#pragma标记-触摸方法
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
UIImage*croppedImg=nil;
UITouch*touch=[触摸任何对象];
CGPoint currentPoint=[触摸位置视图:self.imageView];
double ratioW=imageView.image.size.width/imageView.frame.size.width;
double ratioH=imageView.image.size.height/imageView.frame.size.height;
currentPoint.x*=比率;
currentPoint.y*=比率;
双圈Izew=30*ratioW;
双圈Izeh=30*ratioH;
currentPoint.x=(currentPoint.x-circleSizeW/2在视图或图像视图中添加以下Pangesture
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(imageTaped:)];
[panGesture setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panGesture];
模糊图像的处理方法
- (void)imageTaped:(UIPanGestureRecognizer *)gestureRecognizer
{
CIContext *context = [CIContext contextWithOptions:nil];
CGPoint touchLocation = [gestureRecognizer locationInView:self.imgviewMain];
CGRect temp=CGRectMake(touchLocation.x-25, ((self.imgviewMain.frame.size.height-50) - touchLocation.y)+25, 50, 50);
CIImage *inputImage0 = [[CIImage alloc] initWithImage:self.imgviewMain.image];
CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageWithCGImage:[context createCGImage:inputImage0 fromRect:temp]]];
CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
[blurFilter setDefaults];
[blurFilter setValue: inputImage forKey: @"inputImage"];
[blurFilter setValue: [NSNumber numberWithFloat:0.1]
forKey:@"inputRadius"];
CIImage *outputImage = [blurFilter valueForKey: @"outputImage"];
UIImageView *imgtest=[[UIImageView alloc]init];
imgtest.image=[UIImage imageWithCGImage:[context createCGImage:outputImage fromRect:outputImage.extent]];
UIImage *image;
UIImage *bottomImage = self.imgviewMain.image;
image = imgtest.image;
CGSize newSize = CGSizeMake(self.imgviewMain.frame.size.width, self.imgviewMain.frame.size.height);
UIGraphicsBeginImageContext( newSize );
[bottomImage drawInRect:CGRectMake(0,0,768,1024)];
CGRect newRect2=CGRectMake(temp.origin.x,((self.imgviewMain.frame.size.height-50) - temp.origin.y), image.size.width, image.size.height);
image=[self makeRoundedImage:image radius:10];
[image drawInRect:newRect2 blendMode:kCGBlendModeNormal alpha:0.5];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imgviewMain.image=newImage;
}
圆形成像法
-(UIImage *)makeRoundedImage:(UIImage *) image
radius: (float) radius;
{
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageLayer.contents = (id) image.CGImage;
imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;
UIGraphicsBeginImageContext(image.size);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}
我知道我参加聚会有点晚了,但这可能会帮助其他努力工作的人
我最近需要实现它,但我在互联网上找不到关于这个特定主题的太多信息(特别是如何在处理不同UIImageView的缩放类型(如填充比例、居中比例等)时实现它)。因此,我开发了一个小项目,完全符合您的期望
我设法为基于OSX的项目制作了insta blur。
OSX和iOS的图形和图形上下文管理代码可能不同。
希望这能帮助其他想要做类似事情的macOS开发人员
在这里,当用户拖动鼠标时,我们裁剪图像并将其覆盖在主imageview上
- (void)mouseDragged:(NSEvent *)theEvent {
NSPoint currentPoint = [self.view convertPoint:[theEvent locationInWindow] fromView:nil];
NSPoint currentPointForOverLay = currentPoint;
double ratioW=self.imageView.image.size.width/ self.imageView.frame.size.width ;
double ratioH=self.imageView.image.size.height/self.imageView.frame.size.height;
double circleSizeW = 30; //Width and Height of Rect
double circleSizeH = 30;
currentPointBackup.x *= ratioW;
currentPointBackup.y *= ratioH;
CGRect overLayRect = CGRectMake(currentPointBackup.x , currentPointBackup.y, circleSizeW * ratioW, circleSizeH * ratioH);
CGRect cropRect = CGRectMake(currentPoint.x, currentPoint.y, circleSizeW, circleSizeH);
//NSLog(@"x %0.0f, y %0.0f, width %0.0f, height %0.0f", cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
//Logic to CROP Image on OSX
NSSize ratio = [(NSImageView*)self.imageExtractor.canvasController.contentView originalImageRatio];
CGFloat x = cropRect.origin.x / ratio.width;
CGFloat y = cropRect.origin.y / ratio.height;
CGFloat w = cropRect.size.width / ratio.width;
CGFloat h = cropRect.size.height / ratio.height;
NSRect sourceFrame = NSMakeRect(x, y, w, h);
NSRect targetFrame = NSMakeRect(0, 0, w*1.0f, h*1.0f);
NSImage *targetImage = [[NSImage alloc] initWithSize:targetFrame.size];
if(targetImage.size.width > 0 && targetFrame.size.height > 0) {
[targetImage lockFocus];
[[(NSImageView*)self.imageExtractor.canvasController.contentView originalImage] drawInRect:targetFrame fromRect:sourceFrame operation:NSCompositingOperationCopy fraction:1.0f];
[targetImage unlockFocus];
}
//End CROP Image on OSX
//Apply filters
NSData *data = targetImage.TIFFRepresentation;
CIImage *croppedCIImage = [[CIImage alloc] initWithData:data];
CIImage *filteredCIImage;
if (self.filterTypeOnCanvas == BlurFilterMode)
{
CGFloat filterValue = self.blurSlider.floatValue;
filteredCIImage = [filterManager getBlurredImage:croppedCIImage withAmount:filterValue];
}
else if (self.filterTypeOnCanvas == PixilateFilterMode)
{
CGFloat filterValue = self.pixillateSlider.floatValue;
filteredCIImage = [filterManager getPixellatedImage:croppedCIImage withAmount:filterValue];
}
CGImageRef cgimgRef = [context createCGImage:filteredCIImage
fromRect:[croppedCIImage extent]];
NSImage *blurredNSImage = [[NSImage alloc] initWithCGImage:cgimgRef size:cropRect.size];
//Finally overlay the blurred image on main image
NSImage *newImage = [imageUtils overlayImages:self.imageView.image overlayImage:blurredNSImage overLayRect:nsRect];
self.imageView.image = newImage;
}
叠加图像逻辑
应用过滤器
你说的“矩形或圆形”是什么意思?你想让特定区域模糊成圆形吗?是的…我想模糊特定区域矩形/圆形形状的区域和外观在应用程序的整个生命周期内是固定的还是动态的?试试这个我两周前一直在寻找同一个问题,但找不到合适的解决方案,我做了很多工作,得到了一个答案这是我自己做的。谢谢你的帮助…但我得到了错误…我不能用GaussianBru9方法获得图像好的…问题很清楚…衷心感谢你的帮助…但是如果我想显示用户选择的矩形部分来模糊这个区域??这是怎么可能的?再次感谢UIImage分类类方法。我在描述中提到过leSizeH,circleSizeW正在控制裁剪图像的大小。您可以将30x30更改为50x30,或将半径值4更改为0以获得矩形。感谢您的帮助……但在您的代码中,我需要添加哪个框架??#导入???或其他??是#导入,还包括CoreImage和QuartzCore框架,这不提供回答这个问题。若要评论或要求作者澄清,请在他们的帖子下方留下评论。@VojtechVrbka我不同意你的观点。这个问题特别要求一种方法来模糊图像的一部分,这正是我的例子所做的。我创建这个工作示例并公开它的目的是帮助未来的程序员寻找这个主题。更不用说,公认的答案不能处理不同UIImageView的缩放类型,这使得我的解决方案比公认的解决方案更适合作为参考。@VojtechVrbka AFAIK您可以发布到您的项目的链接,只要您添加免责声明,说明您是一直以来,在许多不同的线程中使用。如果您仍然不同意我的观点,我建议您打开一个关于Meta的线程,讨论这是否是一个好的实践。您应该在答案中直接包含工作示例。一旦链接过时,您的答案将不会给出任何有价值的反馈。@VojtechVrbka我做了计算,发现.m和.h文件都有1517行代码,这太多了,无法包含在一个答案中阅读。我同意你的观点,链接有被废弃的风险。但是,在整个世纪年代,有几十个答案指向各种文档和教程O可能发生相同的问题。
-(UIImage *)makeRoundedImage:(UIImage *) image
radius: (float) radius;
{
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageLayer.contents = (id) image.CGImage;
imageLayer.masksToBounds = YES;
imageLayer.cornerRadius = radius;
UIGraphicsBeginImageContext(image.size);
[imageLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundedImage;
}
- (void)mouseDragged:(NSEvent *)theEvent {
NSPoint currentPoint = [self.view convertPoint:[theEvent locationInWindow] fromView:nil];
NSPoint currentPointForOverLay = currentPoint;
double ratioW=self.imageView.image.size.width/ self.imageView.frame.size.width ;
double ratioH=self.imageView.image.size.height/self.imageView.frame.size.height;
double circleSizeW = 30; //Width and Height of Rect
double circleSizeH = 30;
currentPointBackup.x *= ratioW;
currentPointBackup.y *= ratioH;
CGRect overLayRect = CGRectMake(currentPointBackup.x , currentPointBackup.y, circleSizeW * ratioW, circleSizeH * ratioH);
CGRect cropRect = CGRectMake(currentPoint.x, currentPoint.y, circleSizeW, circleSizeH);
//NSLog(@"x %0.0f, y %0.0f, width %0.0f, height %0.0f", cropRect.origin.x, cropRect.origin.y, cropRect.size.width, cropRect.size.height);
//Logic to CROP Image on OSX
NSSize ratio = [(NSImageView*)self.imageExtractor.canvasController.contentView originalImageRatio];
CGFloat x = cropRect.origin.x / ratio.width;
CGFloat y = cropRect.origin.y / ratio.height;
CGFloat w = cropRect.size.width / ratio.width;
CGFloat h = cropRect.size.height / ratio.height;
NSRect sourceFrame = NSMakeRect(x, y, w, h);
NSRect targetFrame = NSMakeRect(0, 0, w*1.0f, h*1.0f);
NSImage *targetImage = [[NSImage alloc] initWithSize:targetFrame.size];
if(targetImage.size.width > 0 && targetFrame.size.height > 0) {
[targetImage lockFocus];
[[(NSImageView*)self.imageExtractor.canvasController.contentView originalImage] drawInRect:targetFrame fromRect:sourceFrame operation:NSCompositingOperationCopy fraction:1.0f];
[targetImage unlockFocus];
}
//End CROP Image on OSX
//Apply filters
NSData *data = targetImage.TIFFRepresentation;
CIImage *croppedCIImage = [[CIImage alloc] initWithData:data];
CIImage *filteredCIImage;
if (self.filterTypeOnCanvas == BlurFilterMode)
{
CGFloat filterValue = self.blurSlider.floatValue;
filteredCIImage = [filterManager getBlurredImage:croppedCIImage withAmount:filterValue];
}
else if (self.filterTypeOnCanvas == PixilateFilterMode)
{
CGFloat filterValue = self.pixillateSlider.floatValue;
filteredCIImage = [filterManager getPixellatedImage:croppedCIImage withAmount:filterValue];
}
CGImageRef cgimgRef = [context createCGImage:filteredCIImage
fromRect:[croppedCIImage extent]];
NSImage *blurredNSImage = [[NSImage alloc] initWithCGImage:cgimgRef size:cropRect.size];
//Finally overlay the blurred image on main image
NSImage *newImage = [imageUtils overlayImages:self.imageView.image overlayImage:blurredNSImage overLayRect:nsRect];
self.imageView.image = newImage;
}
- (NSImage *) overlayImages:(NSImage *)backgroundImage overlayImage:(NSImage *)overlayImage
overLayRect:(NSRect)overLayRect
{
NSImage *newImage = [[NSImage alloc] initWithSize:[backgroundImage size]];
[newImage lockFocus];
CGRect newImageRect = CGRectZero;
newImageRect.size = [newImage size];
[backgroundImage drawInRect:newImageRect];
[overlayImage drawInRect:overLayRect];
[newImage unlockFocus];
return newImage;
}
-(CIImage *)getBlurredImage:(CIImage *)img withAmount:(float)intensity {
CIFilter *blurFilter = [CIFilter filterWithName:@"CIDiscBlur"];
[blurFilter setValue:img forKey:kCIInputImageKey];
[blurFilter setValue:@(intensity) forKey:@"inputRadius"];
return blurFilter.outputImage;
}
-(CIImage *)getPixellatedImage:(CIImage *)img withAmount:(float)scale
{
CIFilter *pixilateFilter = [CIFilter filterWithName:@"CIPixellate"];
[pixilateFilter setValue:img forKey:kCIInputImageKey];
[pixilateFilter setValue:@(scale) forKey:@"inputScale"];
return pixilateFilter.outputImage;
}