目标C中的用户自定义裁剪

时间:2014-11-05 10:24:56

标签: 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.Now我需要的是当用户上传图像时。如果图像上传成功,则打开alertView“如果要裁剪图像”。单击裁剪按钮后,我需要打开矩形裁剪视图。

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

3 个答案:

答案 0 :(得分:1)

你需要做的很简单。首先创建一个你要裁剪的裁剪尺寸的UIScrollView,你可以通过平移手势等使其变得动态。但是我已经完成了一个恒定的裁剪尺寸所以我不会进入那个细节。接下来创建原始图片大小的UIImageView,并将其image proprety设置为该图片。同时将scorllview's content size属性设置为与图像大小完全相同。允许弹跳有一些额外的效果。然后在按下裁剪时,你只需要传递scrollview的内容偏移和你要弯曲的尺寸(这将是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)];

根据您的逻辑,制作矩形以进行裁剪可能存在错误。但你应该对于做什么有一般的想法。如果有什么东西让你感到困惑或者它是否有帮助,请告诉我,所以我可以帮助进一步帮助:)

答案 1 :(得分:0)

要调整图像大小,请使用此

UIGraphicsBeginImageContext(CGSizeMake(200, 200));
[myImage drawInRect:CGRectMake(0, 0, 200, 200)]; // Size of your ne image
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 2 :(得分:0)

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];

然后你需要调整它以适应200x200,你可以尝试这种方法https://stackoverflow.com/a/537697/660907