2012-01-02 19 views

Respuesta

3

No estoy seguro de que desee recortar, pero en su lugar desea enmascarar la imagen. Esto es bastante fácil de hacer, pero eventualmente descubrirá que funciona para algunas imágenes y otras no. Esto se debe a que necesita tener el canal alfa adecuado en la imagen.

Aquí el código que uso, que obtuve de stackoverflow. (Problem with transparency when converting UIView to UIImage)

CGImageRef CopyImageAndAddAlphaChannel(CGImageRef sourceImage) { 
CGImageRef retVal = NULL; 

size_t width = CGImageGetWidth(sourceImage); 
size_t height = CGImageGetHeight(sourceImage); 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

CGContextRef offscreenContext = CGBitmapContextCreate(NULL, width, height, 
                 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst); 

if (offscreenContext != NULL) { 
    CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), sourceImage); 

    retVal = CGBitmapContextCreateImage(offscreenContext); 
    CGContextRelease(offscreenContext); 
} 

CGColorSpaceRelease(colorSpace); 

return retVal; 
} 

- (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage { 
CGImageRef maskRef = maskImage.CGImage; 
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), 
            CGImageGetHeight(maskRef), 
            CGImageGetBitsPerComponent(maskRef), 
            CGImageGetBitsPerPixel(maskRef), 
            CGImageGetBytesPerRow(maskRef), 
            CGImageGetDataProvider(maskRef), NULL, false); 

CGImageRef sourceImage = [image CGImage]; 
CGImageRef imageWithAlpha = sourceImage; 
//add alpha channel for images that don't have one (ie GIF, JPEG, etc...) 
//this however has a computational cost 
// needed to comment out this check. Some images were reporting that they 
// had an alpha channel when they didn't! So we always create the channel. 
// It isn't expected that the wheelin application will be doing this a lot so 
// the computational cost isn't onerous. 
//if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) { 
imageWithAlpha = CopyImageAndAddAlphaChannel(sourceImage); 
//} 

CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask); 
CGImageRelease(mask); 

//release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel 
if (sourceImage != imageWithAlpha) { 
    CGImageRelease(imageWithAlpha); 
} 

UIImage* retImage = [UIImage imageWithCGImage:masked]; 
CGImageRelease(masked); 

return retImage; 
} 

Y que lo llaman así:

customImage = [customImage maskImage:customImage withMask:[UIImage imageNamed:@"CircularMask.png"]]; 

En este caso, estoy usando una máscara circular para hacer una imagen circular. Tendrá que hacer la máscara irregular que se adapte a sus necesidades.

+0

gracias por su reproducción, pero mi problema es otro. Tengo 4 puntos diferentes, como un trapezoide. El usuario también puede cambiar estos puntos y hacer que la forma sea de su estilo. Ahora quiero recortar la imagen de la misma forma que el usuario ha creado draging point –

+0

¿Puedes tomar los 4 puntos, renderizarlos en una máscara y luego utilizar el código anterior? –

+0

lo siento, soy nuevo en el desarrollo de IOS. No puedo entender su punto. Por ejemplo, tengo cuatro puntos CGPoint firstPoint, secondPoint, thirdPoint y fourthPoint de trapezoid, entonces cómo uso el código anterior Gracias –

Cuestiones relacionadas