【发布时间】:2011-09-12 09:36:33
【问题描述】:
我喜欢在 UIImageView 中裁剪图像。您能否提供对我有帮助的完整源代码。
【问题讨论】:
我喜欢在 UIImageView 中裁剪图像。您能否提供对我有帮助的完整源代码。
【问题讨论】:
-(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 中
【讨论】:
如果您需要 UI 控件来裁剪图像,请尝试 SSPhotoCropperViewController。它是一个自定义视图控制器,提供简单、可配置且易于使用的 UI,用于在 iPhone 应用程序中裁剪和缩放照片。
【讨论】:
我曾经在同一个 UIImageview 中裁剪图像
1)存储图片uiimageview
2)取同一个uiimageview,存为Uiimage
3)裁剪图像并恢复它
下面的代码会有所帮助
-(IBAction)setCropItem:(id)sender {
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];
}
【讨论】:
可能对你有帮助
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];
下面是更有用的链接
how to crop image in to pieces programmatically
祝你好运
【讨论】: