2012-01-02 1 views

risposta

3

Non sono sicuro di voler ritagliare, ma invece si desidera mascherare l'immagine. Questo è abbastanza facile da fare ma alla fine scoprirai che funziona per alcune immagini e non per altre. Questo perché è necessario disporre del canale alfa corretto nell'immagine.

Qui il codice che uso, che ho ottenuto da 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; 
} 

E si chiamano in questo modo:

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

In questo caso, sto usando una maschera circolare per creare un'immagine circolare. Dovrai creare la maschera irregolare adatta alle tue esigenze.

+0

grazie per il replay, ma il mio problema è altro. Ho 4 punti diversi come un trapezoide. L'utente può anche cambiare questi punti e far sì che la forma sia proprio stile. Ora voglio ritagliare l'immagine come la stessa forma creata dall'utente draging point –

+0

Puoi prendere i 4 punti, renderli a una maschera e quindi usare il codice sopra? –

+0

scusa sono nuovo nello sviluppo IOS. Non sono in grado di capire il tuo punto. Ad esempio, ho punto CGPoint firstPoint, secondPoint, thirdPoint e fourthPoint di trapezoidale, quindi come utilizzo il codice precedente. Grazie –