图像裁剪从相机拉伸图像

时间:2014-06-13 08:59:27

标签: ios image crop

我正在使用此项目:http://code4app.net/ios/Perfect-Image-Cropper/51de11576803fa4548000004用于裁剪图像。

事实上,对于从图书馆导入的照片而言,它是完美的,但不适用于来自相机的图像。 当从相机中选择图像时,它显示出裁剪但已拉伸。

@interface ImageViewController (){
    ImageCropperView *cropper;
    UIView *containerView;
    UIImageView *finImageView;
    UIButton *removeBtn;
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        // Media is an image
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        if (image != nil) {

            [self resetCropper];
            containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];

            [self.view addSubview:containerView];

            cropper = [[ImageCropperView alloc] initWithFrame:CGRectMake(10, 10, 300, 150)]
            [cropper setup];
            cropper.image = image;

            // Adding the crop button
            UIButton *cropBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [cropBtn addTarget:self action:@selector(cropImage:) forControlEvents:UIControlEventTouchUpInside];
            [cropBtn setTitle:@"Select" forState:UIControlStateNormal];

            cropBtn.frame = CGRectMake(140, 160, 80, 30.0);

            [containerView addSubview:cropBtn];
            [containerView addSubview:cropper];
        }
    }
}

// Action when crop button clicked
- (IBAction)cropImage:(id)sender
{
    [cropper finishCropping];

    UIImage *newImage = cropper.croppedImage;

    finImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 150)];
    finImageView.image = newImage;

    [cropper removeFromSuperview];
    [containerView removeFromSuperview];

    [self.scrollView insertSubview:finImageView belowSubview:self.actionsTab];

    // Adding a button for remove
    removeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    removeBtn.frame = CGRectMake( 0, 0, 22, 22);
    [removeBtn setBackgroundImage:[UIImage imageNamed:@"remove"] forState:UIControlStateNormal];
    [removeBtn addTarget:self action:@selector(removePhoto:) forControlEvents:UIControlEventTouchUpInside];

    [finImageView insertSubview:removeBtn aboveSubview:finImageView];
    finImageView.userInteractionEnabled = YES;
}

- (IBAction)removePhoto:(id)sender{
    [self resetCropper];
    [finImageView removeFromSuperview];
}

- (void)resetCropper
{
    [cropper reset];
    finImageView.image = nil;
    [removeBtn removeFromSuperview];
}

任何帮助都将非常感谢!

0 个答案:

没有答案