Crop UIImage to alpha

后端 未结 3 1735
谎友^
谎友^ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 15:43

    Thanks to user404709 for making all the hard work. Below code also handles retina images and frees the CFDataRef.

    - (UIImage *)trimmedImage {
    
        CGImageRef inImage = self.CGImage;
        CFDataRef m_DataRef;
        m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
    
        UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
    
        size_t width = CGImageGetWidth(inImage);
        size_t height = CGImageGetHeight(inImage);
    
        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;
                }
    
            }
        }
    
    
        CGFloat scale = self.scale;
    
        CGRect cropRect = CGRectMake(left.x / scale, top.y/scale, (right.x - left.x)/scale, (bottom.y - top.y) / scale);
        UIGraphicsBeginImageContextWithOptions( cropRect.size,
                                               NO,
                                               scale);
        [self drawAtPoint:CGPointMake(-cropRect.origin.x, -cropRect.origin.y)
               blendMode:kCGBlendModeCopy
                   alpha:1.];
        UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        CFRelease(m_DataRef);
        return croppedImage;
    }
    

提交回复
热议问题