我尝试在UIImage上得到圆角,到目前为止我读到的,最简单的方法是使用蒙版图像.为此我使用了TheElements iPhone Example中的代码和我找到的一些图像大小调整代码.我的问题是resizedImage总是为nil而且我没有找到错误...
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize
{
CGSize imageSize = [self size];
float width = imageSize.width;
float height = imageSize.height;
// scaleFactor will be the fraction that we'll
// use to adjust the size. For example, if we shrink
// an image by half, scaleFactor will be 0.5. the
// scaledWidth and scaledHeight will be the original,
// multiplied by the scaleFactor.
//
// IMPORTANT: the "targetHeight" is the size of the space
// we're drawing into. The "scaledHeight" is …Run Code Online (Sandbox Code Playgroud) 我一直试图将UIImage掩盖成一个圆圈.我现在正在使用其他答案中流行的代码,但是虽然我得到一个圆形,但它的边缘非常锯齿状而且不平滑.有人可以帮忙吗?我能得到一个完美,流畅的圆圈吗?
我用来创建掩码的代码是:
(UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef imageNoAlpha = image.CGImage;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGFloat width = CGImageGetWidth(imageNoAlpha);
CGFloat height = CGImageGetWidth(imageNoAlpha);
CGContextRef ctxWithAlpha = CGBitmapContextCreate(nil, width, height, 8, 4*width, cs, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(ctxWithAlpha, CGRectMake(0, 0, width, height), imageNoAlpha);
CGImageRef imageWithAlpha = CGBitmapContextCreateImage(ctxWithAlpha);
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
UIImage* retImage= [UIImage imageWithCGImage:masked];
UIImage* retImageFixed = [retImage transparentBorderImage:1];
CGImageRelease(mask);
CGImageRelease(masked);
CGImageRelease(imageWithAlpha);
CGContextRelease(ctxWithAlpha);
CGColorSpaceRelease(cs);
return retImageFixed; …Run Code Online (Sandbox Code Playgroud)