美文网首页
UIIMageView高级处理

UIIMageView高级处理

作者: 前年的邂逅_Jerry | 来源:发表于2018-04-06 11:26 被阅读5次

    一、图片压缩

    1.1 直接格式转换压缩

    UIImagePNGRepresentation(_albumImage);
    UIImageJPEGRepresentation(_albumImage, scale);
    

    1.2 Context重新绘制

    // 通过上下文对图片压缩处理
    - (UIImage*)scaleImage:(UIImage*)image size:(CGSize)imageSize{
       //并把它设置成为当前正在使用的context 
       UIGraphicsBeginImageContext(imageSize);
       //把图片渲染到上下文
       [image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
       //从当前context中创建一个改变大小后的图片
       UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
       //使当前的context出堆栈 
       UIGraphicsEndImageContext();
       return newImage;
    }
    

    二、图片处理

    2.1 图片像素上修改

    - (UIImage *)eocFilter{
        
        CGImageRef imageRef = self.image.CGImage;
        size_t width = CGImageGetWidth(imageRef);
        size_t height = CGImageGetHeight(imageRef);
        size_t bits = CGImageGetBitsPerComponent(imageRef);
        size_t bitsPerRow = CGImageGetBytesPerRow(imageRef);
        CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
        int alphaInfo = CGImageGetAlphaInfo(imageRef);
        
        CGDataProviderRef provideRef = CGImageGetDataProvider(imageRef);
        CFDataRef dataRef = CGDataProviderCopyData(provideRef);
        int lenght = (int)CFDataGetLength(dataRef);
        ///////// 测试 data和const char* 
        NSString *testStr = @"abcdefg123456789";
        NSData *testData = [testStr dataUsingEncoding:NSUTF8StringEncoding];
        const char *testC = [testData bytes];
        for (int i = 0; i < testData.length; i++) {
            printf("i->%d: %c\n", i, testC[i]);
        }
        UInt8 *pixelBuf = (UInt8 *)CFDataGetMutableBytePtr((CFMutableDataRef)dataRef);
        for (int i = 0; i < lenght; i+=4) {
            //////修改原始像素RGB数据
            [self eocImageFilterFormBuf:pixelBuf offset:i];
        }
        
        CGContextRef contextRef = CGBitmapContextCreate(pixelBuf, width, height, bits, bitsPerRow, colorSpace, alphaInfo);
        
        CGImageRef backImageRef = CGBitmapContextCreateImage(contextRef);
        UIImage *backImage = [UIImage imageWithCGImage:backImageRef scale:[UIScreen mainScreen].scale orientation:self.image.imageOrientation];
        
        CFRelease(dataRef);
        CFRelease(contextRef);
        CFRelease(backImageRef);
        return backImage;
    }
    
    // 对像素点进行加工
    - (void)eocImageFilterFormBuf:(UInt8*)pixelBuf offset:(int)offset{
        
        int offsetR = offset;
        int offsetG = offset + 1;
        int offsetB = offset + 2;
        
        int red   = pixelBuf[offsetR];
        int green = pixelBuf[offsetG];
        int blue  = pixelBuf[offsetB];
        
        int gray = (red + green + blue)/3;
        
        pixelBuf[offsetR] = gray;
        pixelBuf[offsetG] = gray;
        pixelBuf[offsetB] = gray;
    }
    

    2.2 Context上修改

    1:01 ~ 1:34
    3 缩放
    3.1 利用系统缩放
    3.2 自己实现

    • 利用上下文,截取的图片为圆。
    - (UIImage*)circleImage:(UIImage*)image{
        // 1 创建上下文
        UIGraphicsBeginImageContext(CGSizeMake(200, 200));
        // 2 获取上下文
        CGContextRef context = UIGraphicsGetCurrentContext();
        // 3 操作上下文
        CGRect rect = CGRectMake(0, 0, 200, 200);
        CGContextAddEllipseInRect(context, rect);
        // 截剪上下文为圆形 闭环的
        CGContextClip(context);
        // 4 把image 渲染到相应的上下文中
        [image drawInRect:CGRectMake(0, 0, 200, 200)];
        UIImage *backImage =  UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return backImage;
    }
    
    • 利用上下文将视图变成图片,见图一。
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
        view.backgroundColor = [UIColor purpleColor];
        [self.view addSubview:view];
        
        UIView * subView1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
        subView1.backgroundColor = [UIColor redColor];
        UIView * subView2 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
        subView2.backgroundColor = [UIColor greenColor];
        [view addSubview:subView1];
        [view addSubview:subView2];
        
        UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,300 , 100, 100)];
        imgView.image = [self imgWithView:view];
        imgView.backgroundColor = [UIColor orangeColor];
        [self.view addSubview:imgView];
        
    }
    
    - (UIImage *)imgWithView:(UIView *)view{
        //1、开始上下文
        UIGraphicsBeginImageContext(view.frame.size);
        //2、获取上下文
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        //3、将视图绘制到上下文中
    //    [view.layer renderInContext:contextRef];
        [view.layer drawInContext:contextRef];
        //4、获取上下文图片
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    }
    
    图一
    • 利用上下文修改,将图片进行颜色混合,见图二:
        UIImage * img = [UIImage imageNamed:@"1.jpg"];
        self.imgView1.image = img;
        //1、创建上下文
        UIGraphicsBeginImageContext(img.size);
        //2、获取上下文
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        NSLog(@"%@",NSStringFromCGSize(img.size));
        //3、将图片绘制到上下文
        [img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
        UIColor *redColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.4];
        CGContextSetFillColorWithColor(contextRef, redColor.CGColor);
        //4、将颜色渲染到上下文
        CGContextSetBlendMode(contextRef, kCGBlendModeNormal);
        //5、设置渲染区域
        CGContextFillRect(contextRef, CGRectMake(0, 0, img.size.width, img.size.height));
        //6、生成图片
        CGImageRef imgRef = CGBitmapContextCreateImage(contextRef);
        self.imgView2.image = [UIImage imageWithCGImage:imgRef];
        //7、退出上下文
        UIGraphicsEndImageContext();
        //释放对象
        CFRelease(imgRef);
    
    图二
    • 截取图片大小,保证填充UIImageView,并且图片不失真。
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
        UIImage * img = [UIImage imageNamed:@"2"];
        imgView.image = [self ct_imageFromImage:img inRect:imgView.bounds];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.backgroundColor = [UIColor redColor];
        [self.view addSubview:imgView];
    }
    
    /**
     *  从图片中按指定的位置大小截取图片的一部分
     *
     *  @param image UIImage image 原始的图片
     *  @param rect  CGRect rect 要截取的区域
     *
     *  @return UIImage
     */
    - (UIImage *)ct_imageFromImage:(UIImage *)image inRect:(CGRect)rect{
        
        //把像 素rect 转化为 点rect(如无转化则按原图像素取部分图片)
        
        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat width = 0;
        CGFloat height = 0;
    
        if (rect.size.width / rect.size.height > image.size.width / image.size.height) {
            //截取图片的整个宽度,高度按比例截取
            x = 0;
            width = image.size.width;
            height = rect.size.height / rect.size.width * image.size.width;
            y = (image.size.height - height) / 2.0;
        }else{
            //截取图片的整个高度,宽度按比例截取
            y = 0;
            height = image.size.height;
            width = rect.size.width / rect.size.height * image.size.height;
            x = (image.size.width - width) / 2.0;
        }
        CGRect dianRect = CGRectMake(x, y, width, height);
        
        //截取部分图片并生成新图片
        CGImageRef sourceImageRef = [image CGImage];
        //需要释放改对象
        CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);
        UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
        return newImage;
    }
    

    2.3 图片缩放

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        self.imgView.image = [UIImage imageNamed:@"1.jpg"];
        [self.scrollView addSubview:self.imgView];
        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchHandel:)];
        [_scrollView addGestureRecognizer:pinchGesture];
    }
    - (void)pinchHandel:(UIPinchGestureRecognizer*)gesture{
        NSLog(@"%f", gesture.scale);
        if (gesture.state == UIGestureRecognizerStateBegan) {
            _preScal = 1;
        }
        float scaleF = gesture.scale - _preScal + 1;
        _preScal = gesture.scale;
        self.imgView.transform = CGAffineTransformScale(self.imgView.transform, scaleF, scaleF);
        [_scrollView setContentSize:self.imgView.frame.size];
    }
    

    相关文章

      网友评论

          本文标题:UIIMageView高级处理

          本文链接:https://www.haomeiwen.com/subject/uawicftx.html