Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 不使用PickerVIew的图像裁剪_Ios_Objective C_Uiimageview - Fatal编程技术网

Ios 不使用PickerVIew的图像裁剪

Ios 不使用PickerVIew的图像裁剪,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,我想将图像裁剪为特定大小。用户应该能够将图像移动到所需位置,然后单击裁剪按钮 当我在谷歌上搜索时,我只找到了用户从PickerController中选择图像的图像裁剪代码示例。我不想使用picker crontroller来选择一个图像,然后对其进行裁剪 我已经准备好在UIImageView中加载一个图像,现在我想点击裁剪按钮,在那里会出现一个网格,这样用户就可以将图像移动到他想要的地方,以便裁剪它 有人能帮我吗 我的代码如下:但它使用了我不想使用的pickerView控制器 - (void)i

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

当我在谷歌上搜索时,我只找到了用户从PickerController中选择图像的图像裁剪代码示例。我不想使用picker crontroller来选择一个图像,然后对其进行裁剪

我已经准备好在
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);

}
@lllep

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

在.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;
}
#导入
#进口
@界面图像CropViewController:UIViewController
{
IBUIView*vwPictureOptions,*vwHeaderView;
IBUIImageView*ivPicture;
IBMUIScrollView*svScroller;
IBUI按钮*btnCrop、*btnNew、*btnSet、*btnDelete;
IBUILabel*lblTitle;
IBUIOutlet活动指示器视图*装载机;
布尔·伊斯梅;
IBUIVIEW*viewHideBottom;
IBUIView*ViewHiderRight;
}
@属性(非原子,强)UIImage*IPacture;
@属性(非原子)BOOL hasCurrentImage、isProfileImage;
@属性(非原子,保留)UIImageView*iPickedPicture;
@属性(非原子,保留)UIImage*imgSelectedPicture;
在.m文件中

#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;
}
#导入“ImageCropViewController.h”
#导入“AppDelegate.h”
#进口
#包括
@接口映像CropViewController()
@结束
@实现ImageCropViewController
@合成所选图像;
-(无效)viewDidLoad{
[超级视图下载];
vwHeaderView.backgroundColor=[uicolorWithred:104/255.0绿色:149/255.0蓝色:204/255.0阿尔法:0.97];
ivPicture.layer.borderWidth=2.0;
ivPicture.layer.borderColor=[UIColor whiteColor].CGColor;//SetColorRGB(104、149、204).CGColor;
//[自我姿态];
}
-(无效)视图显示:(BOOL)动画{
[超级视图显示:是];
CGRect frame=ivPicture.frame;
frame.size.height=frame.size.width;
//frame.size.width=APP_DELEGATE.window.frame.size.width;
ivPicture.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=viewHiderRight.frame;
frame.origin.x=ivPicture.frame.origin.x+ivPicture.frame.size.width;
viewHiderRight.frame=帧;
[自修改图像];
}
-(无效)修改图像{
if(自拍iPickedPicture){
[self.iPickedPicture从SuperView中移除];
}
self.iPickedPicture=[[UIImageView alloc]initWithImage:imgSelectedPicture];
//Pankaj修改
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将子视图带到前面:vwHeaderView];
[self.view-bringsubview-tofront:btnCrop];
哈得皮;
}
-(iAction)Btncruppressed:(id)发件人{
HUD显示文本(@“加载”);
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
α:1.0];
UIImage*croppedImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
self.iPickedPicture=[[UIImageView alloc]initWithImage:cropeImage];
ivPicture.image=self.iPickedPicture.image;
if(自拍iPickedPicture){
[self.iPickedPicture从SuperView中移除];
}
//ivPicture.backgroundColor=[UIColor redColor];
svScroller.hidden=true;
ivPicture.hidden=否;
self.iPickedPicture.hidden=是;
APP_DELEGATE.imgCroppedImage=裁剪图像;
[自执行选择器:@selector(btnBackPressed:)with object:sender afterDelay:1.0];
}
-(UIView*)视图用于缩放缩略视图:(UIScrollView*)缩略视图{
返回self.ipackedpicture;
}

你能解释一下吗。我不明白这是怎么做到的。@lllep,好的,首先看看我的代码,我有一个serone scrollview(svScroller)&设置一个ImageView(iPickedPicture),现在scrollview被设置为ZOOM,所以在那之后,你想裁剪多少设置ZOOM