我喜欢在UIImageView中裁剪图像。你能否提供对我有用的完整源代码。
答案 0 :(得分:2)
它可以帮到你
UIImage* whole = [UIImage imageNamed:@"whole.jpg"]; //I know this image is 300x300
CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, 100, 100));
UIImage* part = [UIImage imageWithCGImage:cgImg];
UIImageView* Croppedimage = [[UIImageView alloc] initWithImage:part];
以下是更有用的链接
祝你好运答案 1 :(得分:2)
我曾经在同一个UIImageview中裁剪图像
1)存储图像uiimageview
2)使用相同的uiimageview并将其存储为Uiimage
3)裁剪图像并将其恢复
以下代码将有所帮助
- (IBAction为)setCropItem:(ID)发送方 {
UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *finishedPicForCrop = UIGraphicsGetImageFromCurrentImageContext();
CGImageRef imageRef = CGImageCreateWithImageInRect([finishedPicForCrop CGImage], CGRectMake(0, 45, 320, 420));
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
[firstImageView removeFromSuperview];
secondImageView = [[UIImageView alloc] initWithImage:img];
[secondImageView setFrame:CGRectMake(0, 0, 320, 460)];
scrollView.contentSize = secondImageView.bounds.size;
[scrollView addSubview:secondImageView];
}
答案 2 :(得分:1)
如果您需要UI控件来裁剪图片,请尝试SSPhotoCropperViewController。它是一个自定义视图控制器,提供简单,可配置且易于使用的UI,用于在iPhone应用程序中裁剪和缩放照片。
答案 3 :(得分:1)
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
StartPoint = [touch locationInView:touch.view];
if (Img_Screen!=nil) {
[Img_Screen removeFromSuperview];
Img_Screen=nil;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
EndPoint = [touch locationInView:touch.view];
[self finishedTouchInside];
}
-(void)finishedTouchInside{
CGFloat width=EndPoint.x-StartPoint.x;
CGFloat hieght=EndPoint.y-StartPoint.y;
CGRect myImageRect = CGRectMake(StartPoint.x, StartPoint.y, width, hieght);
Img_Screen= [[UIImageView alloc] initWithFrame:myImageRect];
CGImageRef imageRef = CGImageCreateWithImageInRect([imgView.image CGImage], myImageRect);
// or use the UIImage wherever you like
[Img_Screen setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
[self.view addSubview:Img_Screen];
imageViewNew.image=[UIImage imageWithCGImage:imageRef];
}
这里你可以在imageView中裁剪,裁剪的图像将显示在ImageViewNew
中