【问题标题】:How to position and crop UIImage in a circle如何在一个圆圈中定位和裁剪 UIImage
【发布时间】:2013-02-05 18:35:56
【问题描述】:

我有一个界面,可让您将图像定位在一个圆圈内,然后当您确定它在正确的位置时,按下裁剪按钮并裁剪图像的可见区域。

在结构上,我有一个包含滚动视图的视图,该滚动视图包含 UIImageView。 第一个视图层有一个形状层作为一个形状为圆形的蒙版。这是我的初始化代码。

- (void)viewDidLoad
{
    [super viewDidLoad];


_buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.height - 136, 320, 136)];

[_buttonView setBackgroundColor:[UIColor whiteColor]];
_saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_saveButton setTitle:@"Save image" forState:UIControlStateNormal];
[_saveButton setBackgroundImage:[[UIImage imageNamed:@"cntnt-button-defult-dark-grey.png"] resizableImageWithNormalCapInsets]  forState:UIControlStateNormal];
[_saveButton addTarget:self action:@selector(saveImageWasPressed:) forControlEvents:UIControlEventTouchUpInside];
[_saveButton setBackgroundImage:[[UIImage imageNamed:@"cntnt-button-pressed-dark-grey.png"] resizableImageWithNormalCapInsets]  forState:UIControlStateHighlighted];
[_saveButton setFrame:CGRectMake(10, 11, 300, 50)];
[_buttonView addSubview:_saveButton];


_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setTitle:@"CANCEL" forState:UIControlStateNormal];
[_cancelButton setBackgroundImage:[[UIImage imageNamed:@"btm-button-defult-grey.png"] resizableImageWithNormalCapInsets]  forState:UIControlStateNormal];
[_cancelButton setBackgroundImage:[[UIImage imageNamed:@"btm-button-pressed-grey.png"] resizableImageWithNormalCapInsets]  forState:UIControlStateHighlighted];
[_cancelButton setFrame:CGRectMake(10, _saveButton.bottom+7, 300, 50)];
[_buttonView addSubview:_cancelButton];


[self.view addSubview:_buttonView];

_topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, _buttonView.yOrigin)];
[self.view addSubview:_topView];


_scrollView = [[UIScrollView alloc]initWithFrame:_topView.bounds];
[self.topView addSubview:_scrollView];
[_scrollView setDecelerationRate:0.0];


_imageView = [[UIImageView alloc]initWithFrame:CGRectZero];
[_scrollView addSubview:_imageView];


UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, (self.topView.height - 300)/2, 300, 300)];
_imageOverlay = [CAShapeLayer layer];
[_imageOverlay setPath:path.CGPath];
[_imageOverlay setFrame:CGRectMake(0, 0, 320, 320)];
[_topView.layer setMask:_imageOverlay];

[_imageView setImage:[UIImage imageNamed:@"portrait.jpg"]];
[_imageView setSize:_imageView.image.size];
[_scrollView setContentOffset:_imageView.center];
[_scrollView setContentSize:_imageView.image.size];



}

按下“_saveButton”时,我希望在 _imageOverlay 的可见部分所在的位置裁剪图像。

如果您对以上内容有任何疑问,请随时提出。 你能帮帮我吗?

【问题讨论】:

    标签: iphone ios ios5 quartz-2d


    【解决方案1】:

    将视图裁剪为圆形后(下面的渲染视图):

    -(UIImage *)retrieveSingletonImage:(UIView *)renderingView
    {
        UIGraphicsBeginImageContextWithOptions(renderingView.bounds.size, renderingView.opaque, 0.0);
    
        [renderingView.layer renderInContext: UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
        return viewImage;
    }
    

    在renderingView上使用QuartzCore层的圆角半径使其成为一个圆(直径/2 =角)

    【讨论】:

    • 我想我理解但是我应该将哪个视图作为参数传递,它会是哪个视图?请记住我上面给出的结构。干杯!
    • 我的代码中的 renderingView 可能是 you_ImageOverlay (我认为)。所以 _imageOverlay.layer.cornerRadius = 不管,然后调用我的代码。
    【解决方案2】:

    我在网上找到了一些可以完成这项工作的 sn-ps。 首先,我在代表我的圆形叠加层的矩形中裁剪图像

    - (UIImage *)getCroppedImage
    {
        float zoomScale = 1.0 / [_scrollView zoomScale];
        CGRect rect;
        rect.origin.x = [_scrollView contentOffset].x * zoomScale;
        rect.origin.y = [_scrollView contentOffset].y * zoomScale;
        rect.size.width =_imageOverlay.bounds.size.width *zoomScale;
        rect.size.height =_imageOverlay.bounds.size.height *zoomScale;
    
        CGImageRef cr = CGImageCreateWithImageInRect([[_imageView image] CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:cr];
        CGImageRelease(cr);
        return [cropped roundedCornerImage:150 borderSize:0];
    }
    

    然后在返回之前,我将图像的四角修圆并再次裁剪。

    @implementation UIImage (RoundedCorner)
    
    // Creates a copy of this image with rounded corners
    // If borderSize is non-zero, a transparent border of the given size will also be added
    // Original author: Björn Sållarp. Used with permission. See: http://blog.sallarp.com/iphone-uiimage-round-corners/
    - (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize {
        // If the image does not have an alpha layer, add one
        UIImage *image = [self imageWithAlpha];
    
        CGFloat scale = [[UIScreen mainScreen] scale];
        UIGraphicsBeginImageContextWithOptions(image.size, !image.hasAlpha, 0);
    
        // Build a context that's the same dimensions as the new size
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0, image.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
    
        // Create a clipping path with rounded corners
        CGContextBeginPath(context);
        [self addRoundedRectToPath:CGRectMake(borderSize, borderSize, image.size.width - borderSize * 2, image.size.height - borderSize * 2)
                           context:context
                         ovalWidth:cornerSize * scale
                        ovalHeight:cornerSize * scale];
        CGContextClosePath(context);
        CGContextClip(context);
    
        // Draw the image to the context; the clipping path will make anything outside the rounded rect transparent
        CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
    
        // Create a CGImage from the context
        UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return roundedImage;
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      相关资源
      最近更新 更多