在iPhone中裁剪图像
答
我用来裁剪在相同的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];
}
答
它可以帮助你
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
好运
答
若y你需要一个UI控件来裁剪图像尝试SSPhotoCropperViewController。这是一个自定义视图控制器,它提供了一个简单,可配置且易于使用的用户界面,用于在iPhone应用程序中裁剪和缩放照片。
答
-(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];
}
这里u能在ImageView的裁剪和裁剪图像将在ImageViewNew在显示
感谢您的建议最后我在上周六找到了答案。它与上面的代码是一样的我使用CGImageCreateWithImageInRect()来裁剪图像,但我用来将uiimageview存储为uiimage,然后在相同的uiimageview中裁剪图像 – NandaKumar 2011-06-13 07:39:21
welcome&very good :-) – NIKHIL 2011-06-13 08:04:54