歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在iOS中特定形狀剪裁圖片的實現

在iOS中特定形狀剪裁圖片的實現

日期:2017/3/1 10:02:19   编辑:Linux編程

在iOS中如何用特定的形狀剪裁覆蓋UIImageView的圖片?效果圖如下:

解決方案:

用兩個圖片和下面的方法實現。 圖片1是被剪裁的圖片,圖片2是需要剪裁的形狀。

- (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 masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];

}

如果要調整尺寸用

UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];

Copyright © Linux教程網 All Rights Reserved