2011-08-17 59 views

回答

1

您可以使用CoreGraphics框架動態地裁剪圖像。 以下是動態圖像裁剪的示例代碼部分。我希望這會對你有所幫助。

- (void)drawMaskLineSegmentTo:(CGPoint)ptTo withMaskWidth:(CGFloat)maskWidth inContext:(NXMaskDrawContext)context{  
     if (context == nil)   
      return;  
     if (context.count <= 0){ 
      [context addObject:[NSValue valueWithCGPoint:ptTo]]; 
      return; 
     }   
     CGPoint ptFrom = [context.lastObject CGPointValue];    
     UIGraphicsBeginImageContext(self.maskImage.size); 
     [self.maskImage drawInRect:CGRectMake(0, 0, self.maskImage.size.width, self.maskImage.size.height)];  
     CGContextRef graphicsContext = UIGraphicsGetCurrentContext();   
     CGContextSetBlendMode(graphicsContext, kCGBlendModeCopy);  
     CGContextSetRGBStrokeColor(graphicsContext, 1, 1, 1, 1);  
     CGContextSetLineWidth(graphicsContext, maskWidth);    
     CGContextSetLineCap(graphicsContext, kCGLineCapRound);  
     CGContextMoveToPoint(graphicsContext, ptFrom.x, ptFrom.y);  
     CGContextAddLineToPoint(graphicsContext, ptTo.x, ptTo.y);  
     CGContextStrokePath(graphicsContext); 
     self.maskImage = UIGraphicsGetImageFromCurrentImageContext();  
     UIGraphicsEndImageContext();   

     UIGraphicsBeginImageContext(self.displayableMaskImage.size);  
     [self.displayableMaskImage drawInRect:CGRectMake(0, 0, self.displayableMaskImage.size.width, self.displayableMaskImage.size.height)];  
     graphicsContext = UIGraphicsGetCurrentContext();  
     CGContextSetBlendMode(graphicsContext, kCGBlendModeCopy);  
     CGContextSetStrokeColorWithColor(graphicsContext, self.displayableMaskColor.CGColor);  
     CGContextSetLineWidth(graphicsContext, maskWidth);  
     CGContextSetLineCap(graphicsContext, kCGLineCapRound);  
     CGContextMoveToPoint(graphicsContext, ptFrom.x, ptFrom.y);  
     CGContextAddLineToPoint(graphicsContext, ptTo.x, ptTo.y);  
     CGContextStrokePath(graphicsContext); 
     self.displayableMaskImage = UIGraphicsGetImageFromCurrentImageContext();  
     UIGraphicsEndImageContext();   
     [context addObject:[NSValue valueWithCGPoint:ptTo]]; 
} 
18

您可以使用下面的簡單代碼來裁剪圖像。您必須傳遞作爲裁剪區域的圖像和CGRect。在這裏,我裁剪圖像,以便獲得原始圖像的中心部分,並且返回的圖像是方形的。

// Returns largest possible centered cropped image. 
- (UIImage *)centerCropImage:(UIImage *)image 
{ 
    // Use smallest side length as crop square length 
    CGFloat squareLength = MIN(image.size.width, image.size.height); 
    // Center the crop area 
    CGRect clippedRect = CGRectMake((image.size.width - squareLength)/2, (image.size.height - squareLength)/2, squareLength, squareLength); 

    // Crop logic 
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect); 
    UIImage * croppedImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return croppedImage; 
} 
+0

太棒了!像iOS 7上的微風一樣工作。謝謝! –

1

的Xcode 5,iOS裝置7和4英寸的屏幕示例:這裏是一個 SimpleImageCropEditor (Project Zip and Source Code Example的開源例子。您可以將圖像裁剪編輯器加載爲模態視圖控制器並重復使用。看代碼,請留下建設性的意見,看看這個例子代碼是否回答了「iOS的圖像裁剪API」這個問題。

演示,是示例源Objective-C代碼,使用UIImagePickerController,@protocol,UIActionSheet,UIScrollView,UINavigationController,MFMailComposeViewController和UIGestureRecognizer。

10

編輯 - 斯威夫特版本

let imageView = UIImageView(image: image) 
imageView.contentMode = .scaleAspectFill 
imageView.clipsToBounds = true 

所有這些解決方案似乎相當複雜,許多人反而會降低質量的圖像。 您可以使用UIImageView的開箱即用方法簡單得多。

Objective-C的

self.imageView.contentMode = UIViewContentModeScaleAspectFill; 
[self.imageView setClipsToBounds:YES]; 
[self.imageView setImage:img]; 

這將根據您爲UIImageView(我已經打電話給我imageView這裏)設定的尺寸裁剪圖像。
就是這麼簡單,比其他解決方案效果更好。

+0

我認爲你的答案只是簡單地在UIImageView的範圍內調整圖像的大小。如果你只關心顯示圖像,這將工作。但是,例如,如果您需要在上載到服務器之前修改圖像,則此方法在此情況下不起作用。 – Andrew

+0

確實如此,但她/他正在尋找一種「動態生成圖像」的方式,因此我的建議在此處完全正確。 – sf89