美文网首页
iOS裁剪掉图片周围透明度为0的部分

iOS裁剪掉图片周围透明度为0的部分

作者: Jamesholy | 来源:发表于2018-06-11 09:21 被阅读17次
    pragma mark - 裁剪掉周围的透明部分
    + (UIImage*)cutAlphaZero:(UIImage*)image {
        
        CGImageRef cgimage = [image CGImage];
        
        size_t width = CGImageGetWidth(cgimage); // 图片宽度
        size_t height = CGImageGetHeight(cgimage); // 图片高度
        
        unsigned char *data = calloc(width * height * 4, sizeof(unsigned char)); // 取图片首地址
        size_t bitsPerComponent = 8; // r g b a 每个component bits数目
        size_t bytesPerRow = width * 4; // 一张图片每行字节数目 (每个像素点包含r g b a 四个字节)
        CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); // 创建rgb颜色空间
        
        CGContextRef context = CGBitmapContextCreate(data, width,height,bitsPerComponent,bytesPerRow,space,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
        
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgimage);
        int top = 0;  // 上边框透明高度
        int left = 0; // 左边框透明高度
        int right = 0; // 右边框透明高度
        int bottom = 0; // 底边框透明高度
        
        for (size_t row = 0; row < height; row++) {
            BOOL find = false;
            for (size_t col = 0; col < width; col++) {
                size_t pixelIndex = (row * width + col) * 4;
                //            int red = data[pixelIndex];
                //            int green = data[pixelIndex+1];
                //            int blue = data[pixelIndex + 2];
                int alpha = data[pixelIndex + 3];
                if (alpha != 0) {
                    find = YES;
                    break;
                }
            }
            if (find) {
                break;
            }
            top ++;
        }
        
        for (size_t col = 0; col < width; col++) {
            BOOL find = false;
            for (size_t row = 0; row < height; row++) {
                size_t pixelIndex = (row * width + col) * 4;
                
                int alpha = data[pixelIndex + 3];
                if (alpha != 0) {
                    find = YES;
                    break;
                }
            }
            if (find) {
                break;
            }
            left ++;
        }
        
        for (size_t col = width - 1; col > 0; col--) {
            BOOL find = false;
            for (size_t row = 0; row < height; row++) {
                size_t pixelIndex = (row * width + col) * 4;
                
                int alpha = data[pixelIndex + 3];
                if (alpha != 0) {
                    find = YES;
                    break;
                }
            }
            if (find) {
                break;
            }
            right ++;
        }
        
        for (size_t row = height - 1; row > 0; row--) {
            BOOL find = false;
            for (size_t col = 0; col < width; col++) {
                size_t pixelIndex = (row * width + col) * 4;
                int alpha = data[pixelIndex + 3];
                if (alpha != 0) {
                    find = YES;
                    break;
                }
            }
            if (find) {
                break;
            }
            bottom ++;
        }
        
    
        CGFloat scale = image.scale;
        CGImageRef newImageRef = CGImageCreateWithImageInRect(cgimage, CGRectMake(left * scale, top *scale, (image.size.width - left - right)*scale, (image.size.height - top - bottom)*scale));
        UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
        // 释放
        CGImageRelease(cgimage);
        CGContextRelease(context);
        CGColorSpaceRelease(space);
        return  newImage;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS裁剪掉图片周围透明度为0的部分

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