2013-04-03 68 views
0

問題: 與縮小的圖像裁剪是好的。 用放大的圖像裁剪顯示上面的圖像應該是什麼。 我在那裏的yOffset是因爲我想要的裁剪方塊從scrollview的下方開始。如何在滾動視圖內捕捉放大的UIImageView以進行裁剪?

代碼:

CGRect rect; 
float yOffset = 84; 
rect.origin.x = floorf([scrollView contentOffset].x * zoomScale); 
rect.origin.y = floorf(([scrollView contentOffset].y + yOffset) * zoomScale); 
rect.size.width = floorf([scrollView bounds].size.width * zoomScale); 
rect.size.height = floorf((320 * zoomScale)); 

if (rect.size.width > 320) { 
    rect.size.width = 320; 
} 

if (rect.size.height > 320) { 
    rect.size.height = 320; 
} 

CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect); 

UIImage *img = imageView.image; //[UIImage imageWithCGImage:cr]; 

UIGraphicsBeginImageContext(rect.size); 

// translated rectangle for drawing sub image 
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, 320.0f, 320.0f); 
NSLog(@"drawRect: %@", NSStringFromCGRect(drawRect)); 
NSLog(@"rect: %@", NSStringFromCGRect(rect)); 

// draw image 
[img drawInRect:drawRect]; 

// grab image 
UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

CGImageRelease(cr); 

[self.delegate imageCropper:self didFinishCroppingWithImage:cropped]; 

我在做什麼,導致該圖像縮放時,得到錯誤的高度?

回答

9
UIImage* imageFromView(UIImage* srcImage, CGRect* rect) 
{ 
    CGImageRef cr = CGImageCreateWithImageInRect(srcImage.CGImage, *rect); 
    UIImage* cropped = [UIImage imageWithCGImage:cr]; 

    CGImageRelease(cr); 
    return cropped; 
} 
-(void) doneEditing 
{ 
    //Calculate the required area from the scrollview 
    CGRect visibleRect; 
    float scale = 1.0f/scrollView.zoomScale; 
    visibleRect.origin.x = scrollView.contentOffset.x * scale; 
    visibleRect.origin.y = scrollView.contentOffset.y * scale; 
    visibleRect.size.width = scrollView.bounds.size.width * scale; 
    visibleRect.size.height = scrollView.bounds.size.height * scale; 


    FinalOutputView* outputView = [[FinalOutputView alloc] initWithNibName:@"FinalOutputView" bundle:[NSBundle mainBundle]]; 
    outputView.image = imageFromView(imageView.image, &visibleRect); 

    [self.navigationController pushViewController:outputView animated:YES]; 
    [outputView release]; 
} 

載入原單圖片:

enter image description here

縮放圖像:

enter image description here

最後拍攝圖像

enter image description here

+0

看起來不錯! 我在圖像的頂部和底部看到一個綠色的邊框。它看起來像寬度和大小是240x240,因爲滾動視圖的最小縮放比例是1.3。 測試..任何建議? – quantumpotato 2013-04-04 14:50:55

+0

在我的答案或在你的..如果我的答案可以幫助你,那麼你能接受它 – 2013-04-05 04:50:13

0

如果你想採取截圖從滾動型的整體視圖(縮放後),你可以這樣做:

UIImage* image = nil; 
UIGraphicsBeginImageContext(self.scrollView.contentSize); 
{ 
    //save previous frames 
    CGPoint savedContentOffset = self.scrollView.contentOffset; 
    CGRect savedFrame = self.scrollView.frame; 
    CGRect imgFrame = self.imageView.frame; 

    //set the frames with current content size 
    self.scrollView.contentOffset = CGPointZero; 
    self.scrollView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height); 
    self.imageView.frame = self.scrollView.frame; 

    //render image now :) 
    [self.scrollView.layer renderInContext: UIGraphicsGetCurrentContext()]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 

    //now set the frames again with old ones :) 
    self.scrollView.contentOffset = savedContentOffset; 
    self.scrollView.frame = savedFrame; 
    self.imageView.frame = imgFrame; 
    [self viewForZoomingInScrollView:self.scrollView]; 
} 
UIGraphicsEndImageContext(); 

//get the documents path 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
//save file as savedImage.png 
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; 

//get the png data from image 
NSData *imageData = UIImagePNGRepresentation(image); 
//write it now 
[imageData writeToFile:savedImagePath atomically:NO];