图像裁剪而不使用PickerVIew

时间:2015-06-18 05:16:46

标签: ios objective-c uiimageview

我想将图像裁剪为特定尺寸。用户应该能够将图像移动到所需位置,然后单击裁剪按钮。

当我用Google搜索时,我只找到了用户从PickerController中选择图像的图像裁剪的代码示例。我不想使用picker crontroller来选择图像然后裁剪它。

我已准备好在UIImageView中加载图像,现在我想点击裁剪按钮,其中会出现一个网格,这样用户就可以将图像移动到他想要的位置,这样他就可以裁剪它。 / p>

有人可以帮助我。

我的代码如下:但它使用了我不想使用的pickerView控制器。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

    [picker dismissModalViewControllerAnimated:YES];



    pictureImageView.image = image;

    CGSize size = [pictureImageView.image size];

    CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);

    NSValue *cropRectValue = [editingInfo objectForKey:@"UIImagePickerControllerCropRect"];

    cropRect = [cropRectValue CGRectValue];

    UIImageWriteToSavedPhotosAlbum(pictureImageView.image, self, nil, nil);

}

1 个答案:

答案 0 :(得分:0)

@lllep,

对于图像裁剪,我使用下面的代码,可能对你有所帮助, 这是XIB的附加图像,

XIB Screen Shot

See how its looks like

.h文件中的

#import <QuartzCore/QuartzCore.h>
#import <AssetsLibrary/AssetsLibrary.h>

@interface ImageCropViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIScrollViewDelegate, UIScrollViewAccessibilityDelegate,UIGestureRecognizerDelegate>
{

     IBOutlet UIView * vwPictureOptions, *vwHeaderView;
     IBOutlet UIImageView * ivPicture;
     IBOutlet UIScrollView * svScroller;
     IBOutlet UIButton * btnCrop, * btnNew, * btnSet, * btnDelete;
     IBOutlet UILabel * lblTitle;
     IBOutlet UIActivityIndicatorView * aiLoader;
    BOOL isImage;
    IBOutlet UIView *viewHideBottom;
    IBOutlet UIView *viewHideRight;
}

@property (nonatomic, strong) UIImage * iPicture;
@property (nonatomic) BOOL hasCurrentImage, isProfileImage;
@property (nonatomic, retain) UIImageView * iPickedPicture;
@property (nonatomic, retain) UIImage * imgSelectedPicture;
<。文件

中的

#import "ImageCropViewController.h"
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#include <CoreFoundation/CoreFoundation.h>

@interface ImageCropViewController ()

@end

@implementation ImageCropViewController

@synthesize imgSelectedPicture;

- (void)viewDidLoad {
  [super viewDidLoad];
    vwHeaderView.backgroundColor = [UIColor colorWithRed:104/255.0 green:149/255.0 blue:204/255.0 alpha:0.97];
    ivPicture.layer.borderWidth =  2.0;
    ivPicture.layer.borderColor = [UIColor whiteColor].CGColor; //SetColorRGB(104, 149, 204).CGColor;
    //[self setSwipeGesture];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    CGRect frame =ivPicture.frame;
    frame.size.height = frame.size.width;
    //frame.size.width = APP_DELEGATE.window.frame.size.width;
    ivPicture.frame = frame;

    frame =svScroller.frame;
   // frame.size.height = APP_DELEGATE.window.frame.size.width;
    frame.size.height = frame.size.width;
    svScroller.frame = frame;

    frame = viewHideBottom.frame;
    frame.origin.y = ivPicture.frame.origin.y+ivPicture.frame.size.height;
    frame.origin.x = 0;
    frame.size.width = APP_DELEGATE.window.frame.size.width;
    frame.size.height = APP_DELEGATE.window.frame.size.height - frame.origin.y-46;
    viewHideBottom.frame = frame;

    frame = viewHideRight.frame;
    frame.origin.x = ivPicture.frame.origin.x+ivPicture.frame.size.width;
    viewHideRight.frame = frame;

    [self modifyImage];
}


-(void)modifyImage {
    if (self.iPickedPicture) {
        [self.iPickedPicture removeFromSuperview];
    }
    self.iPickedPicture = [[UIImageView alloc]initWithImage:imgSelectedPicture];
    //Pankaj modification
    svScroller.minimumZoomScale = MAX(svScroller.bounds.size.width / self.iPickedPicture.image.size.width, svScroller.bounds.size.height / self.iPickedPicture.image.size.height);
    svScroller.maximumZoomScale = MAX(MAX(svScroller.bounds.size.width / self.iPickedPicture.image.size.width, svScroller.bounds.size.height / self.iPickedPicture.image.size.height),1.0);
    svScroller.zoomScale = MAX(svScroller.bounds.size.width / self.iPickedPicture.image.size.width, svScroller.bounds.size.height / self.iPickedPicture.image.size.height );
    [svScroller setContentSize:CGSizeMake(self.iPickedPicture.frame.size.width, self.iPickedPicture.frame.size.height)];
    [svScroller addSubview:self.iPickedPicture];
    //self.iPickedPicture.center = svScroller.center
    [self.view bringSubviewToFront:vwHeaderView];
    [self.view bringSubviewToFront:btnCrop];
    HUDHIDE;
}



-(IBAction)btnCropPressed:(id)sender {
    HUDSHOWWITHTEXT(@"Loading");
    svScroller.layer.borderWidth = 0.0;

    CGRect visibleRect;
    float zoomScale = (1.0 / svScroller.zoomScale);
    visibleRect.origin = svScroller.contentOffset;
    visibleRect.size = svScroller.frame.size;

    visibleRect.origin.x = fabsf(svScroller.contentOffset.x * zoomScale);
    visibleRect.origin.y = fabsf(svScroller.contentOffset.y * zoomScale);
    visibleRect.size.width = fabsf(svScroller.frame.size.width * zoomScale);
    visibleRect.size.height = fabsf(svScroller.frame.size.height * zoomScale);

    UIGraphicsBeginImageContextWithOptions(visibleRect.size,NO,svScroller.zoomScale);
    [self.iPickedPicture.image drawAtPoint:CGPointMake(-visibleRect.origin.x, -visibleRect.origin.y)
                                 blendMode:kCGBlendModeNormal
                                     alpha:1.0];
    UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.iPickedPicture = [[UIImageView alloc]initWithImage:croppedImage];
    ivPicture.image = self.iPickedPicture.image;
    if (self.iPickedPicture) {
        [self.iPickedPicture removeFromSuperview];
    }
    //ivPicture.backgroundColor = [UIColor redColor];
    svScroller.hidden = true;
    ivPicture.hidden = NO;
    self.iPickedPicture.hidden = YES;
    APP_DELEGATE.imgCroppedImage = croppedImage;
    [self performSelector:@selector(btnBackPressed:) withObject:sender afterDelay:1.0];
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)inScroll {
    return self.iPickedPicture;
}