2012-01-02 21 views

cevap

3

Kesmek istediğinizden emin değilim, ancak bunun yerine resmi maskelemek istiyorsunuz. Bu oldukça kolaydır, ancak sonunda bazı görüntüler için değil başkaları için çalıştığını görürsünüz. Bunun nedeni, görüntüde uygun alfa kanalına sahip olmanız gerektiğidir.

Burada kullandığım kod, stackoverflow'tan aldığım kod. (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; 
} 

Ve böyle diyoruz:

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

Bu durumda, ben dairesel resim yapması için bir genelge maske kullanıyorum. İhtiyaçlarınıza uygun düzensiz maskeyi yapmanız gerekir.

+0

Tekrarınız için teşekkürler, ama benim sorunum başka.bir trapezoid gibi bir 4 farklı nokta var.Kullanıcı da bu noktayı değiştirebilir ve şekil kendi stilidir.Şimdi kullanıcı tarafından oluşturduğu aynı şekil gibi kırpmak istiyorum sürüklenme noktası –

+0

4 noktayı alıp maskeye dönüştürür ve sonra yukarıdaki kodu kullanabilir misiniz? –

+0

Üzgünüz, IOS geliştirmede yeniyim.Konumunuzu anlayamıyorum. Örneğin yukarıdaki kodun dört noktalı CGPoint firstPoint, secondPoint, thirdPoint ve fourtPoint'i var. Daha sonra yukarıdaki kodu kullanıyorum Teşekkürler –

İlgili konular