是否存在針對在Xcode項目中動態裁剪圖像的目標C的任何裁剪圖像API?請提供一些技巧或技巧,我如何在iPhone中裁剪相機圖像。適用於iOS的圖像裁剪API
26
A
回答
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;
}
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
這裏)設定的尺寸裁剪圖像。
就是這麼簡單,比其他解決方案效果更好。
相關問題
- 1. xamario ios裁剪圖像像andorid
- 2. 裁剪圖像
- 3. 裁剪圖像
- 4. 圖像裁剪以適合在GridView
- 5. imagick getImageLength不適用於裁剪後圖像
- 6. 如何裁剪圖像左,右僅適用於移動設備
- 7. 無法剪裁/裁剪圖像
- 8. 如何在iOS中裁剪圖像
- 9. Flexbox列裁剪圖像(僅限iOS)
- 10. 圖像裁剪AVCaptureSession圖像
- 11. 關於裁剪/裁剪視圖
- 12. WPF圖像裁剪
- 13. html5圖像裁剪
- 14. WPF圖像裁剪
- 15. 笨裁剪圖像
- 16. 裁剪android圖像
- 17. GWT圖像裁剪
- 18. Silverlight圖像裁剪
- 19. 裁剪YUV圖像
- 20. raphael.js裁剪圖像
- 21. 圖像裁剪PHP
- 22. 使用Java裁剪圖像
- 23. 使用ImageScience裁剪圖像
- 24. 裁剪圖像使用jQuery
- 25. 用OpenCV裁剪圖像
- 26. 使用CGImageCreateWithImageInRect裁剪圖像
- 27. 讓用戶裁剪圖像
- 28. 使用php裁剪圖像
- 29. java中的圖像裁剪
- 30. iPhoto般的圖像裁剪?
太棒了!像iOS 7上的微風一樣工作。謝謝! –