使用圆形叠加层实现UIImagePickerController,如联系人App

时间:2014-12-31 09:45:45

标签: ios uiimagepickercontroller

我希望实现与“应用程序应用程序”(iOS)完全相同的UIImagePickerController,但我无法以与图像上的圆形叠加相同的方式实现此行为。

2 个答案:

答案 0 :(得分:0)

我相信你的意思是UIImageView,而不是UIImagePickerController。如果是这样,你可以这样做:

image.layer.borderWidth=1.0
image.layer.masksToBounds = false
image.layer.borderColor = UIColor.whiteColor().CGColor
image.layer.cornerRadius = 13
image.layer.cornerRadius = image.frame.size.height/2
image.clipsToBounds = true

致以最诚挚的问候,

答案 1 :(得分:0)

要转换UIImagePickerController返回的UIImage,请执行此操作。创建一个名为UIImage + CircleMask的新类。确保它是UIImage的子类。您可以使用您的UIImage,您偏好的角半径或标准image.frame.size.height/2进行自定义。它将返回带有CircleMask的UIImage

@interface UIImage (CircleMask)

+ (UIImage *)roundedRectImageFromImage :(UIImage *)image
                                  size :(CGSize)imageSize
                      withCornerRadius :(float)cornerRadius;

@end

@implementation UIImage(Overlay)

+(UIImage*)roundedRectImageFromImage:(UIImage *)image
                                size:(CGSize)imageSize
                    withCornerRadius:(float)cornerRadius
{
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);   //  <= notice 0.0 as third scale parameter. It is important cause default draw scale ≠ 1.0. Try 1.0 - it will draw an ugly image..
    CGRect bounds=(CGRect){CGPointZero,imageSize};
    [[UIBezierPath bezierPathWithRoundedRect:bounds
                                cornerRadius:cornerRadius] addClip];
    [image drawInRect:bounds];
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return finalImage;
}

@end

UIViewController上,不要忘记添加以下内容:

  #import "UIImage+CircleMask.h"

致以最诚挚的问候,