美文网首页
图片打马赛克

图片打马赛克

作者: Miss_DQ | 来源:发表于2016-10-06 16:28 被阅读38次
    #pragma mark - 马赛克
    - (UIImage *)Mosaic:(UIImage *)inImage Level:(int)level
    {
        unsigned char *imgPixel = RequestImagePixelData(inImage);
        CGImageRef inImageRef = [inImage CGImage];
        GLuint width = CGImageGetWidth(inImageRef);
        GLuint height = CGImageGetHeight(inImageRef);
        unsigned char prev[4] = {0};
        int bytewidth = width*4;
        int i,j;
        int val = level;
        for(i=0;i<height;i++) {
            if (((i+1)%val) == 0) {
                memcpy(imgPixel+bytewidth*i, imgPixel+bytewidth*(i-1), bytewidth);
                continue;
            }
            for(j=0;j<width;j++) {
                if (((j+1)%val) == 1) {
                    memcpy(prev, imgPixel+bytewidth*i+j*4, 4);
                    continue;
                }
                memcpy(imgPixel+bytewidth*i+j*4, prev, 4);
            }
        }
        NSInteger dataLength = width*height* 4;
        //下面的代码创建要输出的图像的相关参数
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imgPixel, dataLength, NULL);
        // prep the ingredients
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
        //创建要输出的图像
        CGImageRef imageRef = CGImageCreate(width, height,
                                            bitsPerComponent,
                                            bitsPerPixel,
                                            bytewidth,
                                            colorSpaceRef,
                                            bitmapInfo,
                                            provider,
                                            NULL, NO, renderingIntent);
        UIImage *my_Image = [UIImage imageWithCGImage:imageRef];
        CFRelease(imageRef);
        CGColorSpaceRelease(colorSpaceRef);
        CGDataProviderRelease(provider);
        return my_Image;
    }
    

    相关文章

      网友评论

          本文标题:图片打马赛克

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