2015-06-18 21 views
0

我想裁剪圖像到特定的大小。用戶應該能夠將圖像移動到所需的位置,然後單擊裁剪按鈕。圖像裁剪不使用PickerVIew

當我谷歌搜索我只發現圖像作物的用戶從PickerController中選擇圖像的代碼示例。我不想使用選取器控制器來選擇圖像,然後裁剪它。

我已經準備好加載UIImageView中的一張圖片,現在我想單擊網格出現的剪裁按鈕,這樣用戶就可以將圖片移動到他想要的位置,以便剪裁它。

有人可以幫助我。

我的代碼如下:但它使用了我不想使用的控制器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); 

} 

回答

0

@lllep,

進行圖像裁切,我用下面的代碼,可能會對你有所幫助, 下面是附加的圖像爲廈門國際銀行,

XIB Screen Shot

See how its looks like

in .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; 

在.m文件

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

你能解釋一下。我不明白這是如何完成的。 – Illep

+0

@lllep,好吧,首先看到我的代碼,我有一個滾動視圖(svScroller)&設置一個ImageView(iPickedPicture),現在scrollview被設置爲ZOOM,所以在那之後,你想裁剪多少設置使用滾動視圖縮放縮放在屏幕中。所以當你點擊按鈕** btnCropPressed **後,你可以從你的原始圖像獲取裁剪圖像。瞭解:-) – Mehul