简体   繁体   中英

Image Crop without using PickerVIew

I want to crop an image to a particular size. The user should be able to move the image to the desired position and then click on the Crop button.

When i googled i only found code examples of image crop where the user selects an image from the PickerController. I don't want to use the picker crontroller to select an image and then Crop it.

I have all ready loaded an image in the UIImageView , and i now want to click on the crop button where a grid will appear so the user can move the image where he wants so he could crop it.

Can some one help me here.

My code is as follows: but it has used the pickerView Controller which i don't want to use.

- (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);

}

@lllep,

for image crop, i have used below code,may be helpful to you, Here is Attached Image for XIB,

XIB屏幕截图

看看它看起来如何

in .h File

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

in .m File

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM