ios - Objective-C 中的用户自定义裁剪

标签 ios objective-c uiimageview uiimagepickercontroller crop

我像这样使用 UIImagePickerController 为 imageView 获取图像

 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *originalImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    CGSize destinationSize1 = CGSizeMake(200, 200);
    UIGraphicsBeginImageContext(destinationSize1);
    [originalImage drawInRect:CGRectMake(0,0,destinationSize1.width,destinationSize1.height)];
    UIImage *newImage1 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    firstimage1.image = newImage1;
    [picker dismissViewControllerAnimated:YES completion:nil];

    NSLog(@"image width is %f",firstimage1.image.size.width);
    NSLog(@"image height is %f",firstimage1.image.size.height);
   if (firstimage1.image.size.width==200 && firstimage1.image.size.height==200) {
    UIAlertView *alertcrp=[[UIAlertView alloc]initWithTitle:@"message!" message:@"you want to crop this image" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
       [alertcrp addButtonWithTitle:@"Crop "];
        [alertcrp show];
        }
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"Button Index =%ld",(long)buttonIndex);
    if (buttonIndex == 0)
    {
        NSLog(@"You have clicked Cancel");
    }
    else if(buttonIndex == 1)
    {
   UIImagePickerController *controller = [[UIImagePickerController alloc] init];

        controller.allowsEditing = YES; // this will allow you to crop the image first
        controller.delegate = self;

        [self presentViewController:controller animated:YES completion:nil];

    }
}

我们将图像大小调整为 200*200。现在我需要的是当用户上传图像时。如果图像上传成功,然后打开 alertView“如果你想裁剪图像”。单击裁剪按钮后,我需要打开矩形裁剪 View 。

请告诉我关于我的问题的任何想法。感谢高级。

最佳答案

您需要做的非常简单。首先创建一个 UIScrollView 你想要裁剪的裁剪尺寸,如果你想通过平移手势等你可以让它动态。但我已经完成了一个恒定的裁剪尺寸所以我不会详细介绍.接下来创建一个原始图像大小的 UIImageView 并将其 image 属性 设置为该大小。还要将 scorllview 的内容大小 属性设置为与图像大小完全相同。让反弹产生一些额外的效果。然后按下裁剪键,您只需将 ScrollView 的 contentoffset 和您想要裁剪的大小(即 ScrollView 的大小,即它的宽度和高度)传递给此函数。

- (UIImage *)crop:(UIImage *)image inRect:(CGRect)cropRect
{
    CGImageRef cropRef = CGImageCreateWithImageInRect(image.CGImage, cropRect);
    UIImage* cropImage = [UIImage imageWithCGImage:cropRef];
    CGImageRelease(cropRef);

    return cropImage;
}

你可以按照上面的描述调用这个方法

UIImage * img = [[CommonFunctions sharedManager] crop:imgView.image inRect:CGRectMake(scrollView.contentOffset.x , scrollView.contentOffset.y , reqWidth, reqHeight)];

根据您的逻辑制作裁剪矩形可能存在错误。但是你应该对做什么有大致的了解。如果有什么让您感到困惑或者是否有帮助,请告诉我,这样我也许可以进一步提供帮助:)

关于ios - Objective-C 中的用户自定义裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26754775/

相关文章:

ios - 从容器 View 中抓取数据

ios - Adobe Air 23 + iOS 10.1.1 全面屏

iPhone:显示带有导航栏的模态 UITableViewController

ios - Swift:IBoutlet 总是返回 nil 对象?

ios - 在应用程序 IOS Objective C 启动时添加新的 View Controller

iphone - 如何将 NSArray 发送到 Web 服务

ios - 如何防止 sizeToFit 更改 UILabel 宽度?

ios - 在 IB 上调整 UIImage 大小的问题

iphone - 无法移动我的角色

objective-c - UIImageView 动画从左到右