2011-06-10 150 views

回答

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

它可以幫助你

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

好運

+1

感謝您的建議最後我在上週六找到了答案。它與上面的代碼是一樣的我使用CGImageCreateWithImageInRect()來裁剪圖像,但我用來將uiimageview存儲爲uiimage,然後在相同的uiimageview中裁剪圖像 – NandaKumar 2011-06-13 07:39:21

+0

welcome&very good :-) – NIKHIL 2011-06-13 08:04:54

1

若y你需要一個UI控件來裁剪圖像嘗試SSPhotoCropperViewController。這是一個自定義視圖控制器,它提供了一個簡單,可配置且易於使用的用戶界面,用於在iPhone應用程序中裁剪和縮放照片。

這裏是tutorialsource code on GitHub

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]; 

}

這裏u能在ImageView的裁剪和裁剪圖像將在ImageViewNew在顯示