添加放大/缩小和裁剪照片

时间:2016-09-13 10:21:09

标签: ios objective-c

我正在尝试添加放大/缩小功能并在保存更改之前裁剪个人资料图片,但我不知道该怎么做。以下是我的相关代码:

- (void)viewDidLoad {

    ...

    NSString *urlString = [User sharedUser].avatar;
    NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    [_avatarImageView setImageWithURLRequest:imageRequest placeholderImage:nil success:^(NSURLRequest *request,   NSHTTPURLResponse *response, UIImage *image) {
        _avatarImageView.image = image;
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    _avatarImageView.layer.masksToBounds = YES;
    _avatarImageView.layer.cornerRadius = _avatarImageView.frame.size.width / 2; //create circle image

    [self initImagePicker];

    ...

}

- (void) initImagePicker {
    myPicker = [[MyImagePicker alloc] init];
    myPicker.SourceVC = self;
    myPicker.SourceView = self.view;
    myPicker.delegate = self;
    myPicker.isImage = YES;

    [myPicker initImagePicker];
}

- (IBAction) ChooseImageSourceAlbum {

    tempType = POPUP_TYPE_IMAGPICKER;
    selectArray = @[@"Camera",@"Choose Photos",@"Delete Photo"];

    //Call popup
    PopupTemplateViewController *vc = [[PopupTemplateViewController alloc] initWithNibName:@"PopupTemplateViewController" bundle:nil];
    vc.delegate = self;
    vc.dataArray = selectArray;
    vc.type = POPUP_TYPE_OTHERS;
    self.useBlurForPopup = NO;

    [self presentPopupViewController:vc animated:YES completion:nil];
}

- (void) MyImagePickerTakePicture:(NSDictionary *)_dic {

    NSString *mediaType = [_dic objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]) {
        UIImage *image = [_dic objectForKey:UIImagePickerControllerOriginalImage];

        //??? add crop and zoom image here!!!

       [self performSelector:@selector(processImage:) withObject:image afterDelay:0.25f];

    }
}

- ( void )processImage:( UIImage * )image {

    CGFloat ratio = 1.0;
    if (image.size.width > 400) {
        ratio = 400 / image.size.width;
    }
    else if (image.size.height > 600) {
        ratio = 600 / image.size.height;
    }

    UIImage *imageResize = [self scaleImage:image toScale:ratio];
    [_avatarImageView setImage:imageResize];

    [self UpdateAvatar];

}

- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return scaledImage;
}

- (void) UpdateAvatar {
    [self showSeachingPopViewinView:self.view type:POPUP_TYPE_UPDATE];

    NSString *account = [[User sharedUser] account];

    NSMutableDictionary *parameters = [NSMutableDictionary new];
    [parameters setObject:account forKey:@"account"];
    [parameters setObject:_avatarImageView.image forKey:@"avatar"];

    [GatewayManager callUpdateAvatar:parameters delegate:self];
}

- (void)popupView:(PopupTemplateViewController *)popupView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self dismissPopupViewControllerAnimated:NO completion:nil];
    NSString *str = selectArray[indexPath.row];
    if ([str isEqualToString:@"Camera"]) {
        [myPicker OpenCamera];
    }
    else if ([str isEqualToString:@"Choose Photos"]) {
        [myPicker OpenAlbum];
    } else {
        [self deletePhoto];
    }
}

现在当我选择个人资料图片时,它会弹出显示3个选项的视图:相机,选择照片,删除照片。我想要实现的是,在拍摄照片或选择照片后,我想要另一张照片,我可以放大/缩小和裁剪。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了允许编辑的图片选择器属性,因此我在 初始图片选择器 中添加了这个:

imagePicker.allowsEditing = YES; //allows image to be editted before choosing

同样在 MyImagePickerTakePicture 中,我将UIImagePickerControllerOriginalImage更改为UIImagePickerControllerEditedImage

- (void) MyImagePickerTakePicture:(NSDictionary *)_dic {

    NSString *mediaType = [_dic objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]) {
        UIImage *image = [_dic objectForKey:UIImagePickerControllerOriginalImage];

        //??? add crop and zoom image here!!!

       [self performSelector:@selector(processImage:) withObject:image afterDelay:0.25f];

    }
}