2012-01-02 39 views

Trả lời

3

Tôi không chắc chắn bạn muốn cắt, nhưng thay vào đó bạn muốn che hình ảnh. Điều này là khá dễ dàng để làm nhưng cuối cùng bạn sẽ thấy rằng nó hoạt động cho một số hình ảnh và không phải những người khác. Điều này là do bạn cần có kênh alpha thích hợp trong hình ảnh.

Ở đây mã tôi sử dụng, mà tôi nhận được từ 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; 
} 

Và bạn gọi nó như thế này:

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

Trong trường hợp này, tôi đang sử dụng một mặt nạ hình tròn để tạo ra một hình ảnh tròn. Bạn sẽ cần phải làm cho mặt nạ bất thường phù hợp với nhu cầu của bạn.

+0

cảm ơn bạn đã phát lại nhưng vấn đề của tôi là khác. Tôi có 4 điểm khác nhau như hình thang. Người dùng cũng có thể thay đổi điểm này và tạo hình dạng theo phong cách riêng.Bây giờ tôi muốn cắt hình ảnh giống với hình dạng mà người dùng đã tạo draging point –

+0

Bạn có thể lấy 4 điểm, đưa chúng lên mặt nạ, và sau đó sử dụng mã trên không? –

+0

xin lỗi Tôi mới phát triển IOS.Tôi không thể hiểu được quan điểm của bạn. Ví dụ tôi có bốn điểm điểm đầu tiên của CGPoint, secondPoint, thirdPoint và fourthPoint của hình thang, sau đó làm thế nào tôi sử dụng mã ở trên Cảm ơn –

Các vấn đề liên quan