Crop UIImage to alpha

后端 未结 3 1737
谎友^
谎友^ 2020-12-31 14:56

I have a rather large, almost full screen image that I\'m going to be displaying on an iPad. The image is about 80% transparent. I need to, on the client, determine the boun

3条回答
  •  独厮守ぢ
    2020-12-31 15:36

    I created a category on UImage which does this if any one needs it...

    + (UIImage *)cropTransparencyFromImage:(UIImage *)img {
    
        CGImageRef inImage = img.CGImage;           
        CFDataRef m_DataRef;  
        m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));  
        UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);  
    
        int width = img.size.width;
        int height = img.size.height;
    
        CGPoint top,left,right,bottom;
    
        BOOL breakOut = NO;
        for (int x = 0;breakOut==NO && x < width; x++) {
            for (int y = 0; y < height; y++) {
                int loc = x + (y * width);
                loc *= 4;
                if (m_PixelBuf[loc + 3] != 0) {
                    left = CGPointMake(x, y);
                    breakOut = YES;
                    break;
                }
            }
        }
    
        breakOut = NO;
        for (int y = 0;breakOut==NO && y < height; y++) {
    
            for (int x = 0; x < width; x++) {
    
                int loc = x + (y * width);
                loc *= 4;
                if (m_PixelBuf[loc + 3] != 0) {
                    top = CGPointMake(x, y);
                    breakOut = YES;
                    break;
                }
    
            }
        }
    
        breakOut = NO;
        for (int y = height-1;breakOut==NO && y >= 0; y--) {
    
            for (int x = width-1; x >= 0; x--) {
    
                int loc = x + (y * width);
                loc *= 4;
                if (m_PixelBuf[loc + 3] != 0) {
                    bottom = CGPointMake(x, y);
                    breakOut = YES;
                    break;
                }
    
            }
        }
    
        breakOut = NO;
        for (int x = width-1;breakOut==NO && x >= 0; x--) {
    
            for (int y = height-1; y >= 0; y--) {
    
                int loc = x + (y * width);
                loc *= 4;
                if (m_PixelBuf[loc + 3] != 0) {
                    right = CGPointMake(x, y);
                    breakOut = YES;
                    break;
                }
    
            }
        }
    
    
        CGRect cropRect = CGRectMake(left.x, top.y, right.x - left.x, bottom.y - top.y);
    
        UIGraphicsBeginImageContextWithOptions( cropRect.size,
                                               NO,
                                               0.);
        [img drawAtPoint:CGPointMake(-cropRect.origin.x, -cropRect.origin.y)
               blendMode:kCGBlendModeCopy
                   alpha:1.];
        UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return croppedImage;
    }
    

提交回复
热议问题