iphone - 如何在一个圆圈中定位和裁剪 UIImage

标签 iphone ios ios5 quartz-2d

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

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

- (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 的可见部分所在的位置裁剪图像。

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

最佳答案

将 View 裁剪为圆形后(下面的 renderingView):

-(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 = 角)

关于iphone - 如何在一个圆圈中定位和裁剪 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985284/

相关文章:

iphone - 在 iOS 6 和 iOS 7 中更改不同的 UIImage 颜色

ios - 为什么我的 native react 应用程序在录制时在后台终止(iOS,RN 0.63.3,Expo-Av 9.2.3)

ios - Xcode Interface Builder 单独的 ViewController 来控制一个 subview

ios5 - 如果设备是ios 5

iphone - Skybox OpenGL ES iPhone和iPad

iphone - 数字签名是否被视为加密?

iphone - Objective-C:为什么在导入头文件时出现编译错误?

ios - 实例将立即被释放,因为属性 'imageView' 是 'weak'

ios - 企业无线分发不工作 iOS 5.1.1

ios - UITableView 的第一部分被 viewForHeaderInSection 隐藏