Xcodeプロジェクトで画像を動的にトリミングする対象C用のクロッピング画像APIはありますか?どのように私はiPhoneでカメラの画像をトリミングすることができますいくつかのトリックやテクニックを提供してください。Image iOS用クロッピングAPI
答えて
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]];
}
以下の簡単なコードを使用して画像をトリミングすることができます。画像と切り取り領域である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;
}
のXcode 5、IOSの7、及び4インチの画面例:ここ SimpleImageCropEditor (Project Zip and Source Code Exampleのオープンソースの一例です。 Image Crop Editorをモーダルビューコントローラとしてロードして再利用することができます。コードを見て、このサンプルコードが「iOS向け画像クロッピングAPI」の質問に答えるかどうかについて建設的なコメントを残してください。
例のソースObjective-Cコード、UIImagePickerController、@protocol、UIActionSheet、UIScrollView、UINavigationController、MFMailComposeViewController、およびUIGestureRecognizerの使用例です。
EDIT - スウィフトバージョン
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. Image CodeIgniterとCropperを使用したクロッピング
- 2. Image Codeigniterでクロッピングする
- 3. FFMPEG APIとクロッピング
- 4. UIImage(CoreGraphics、iOS)をクロッピングするときのパフォーマンスの問題
- 5. OCR on image - iOS
- 6. Carrierwaveクロッピング
- 7. iOS Firebase Image @ 2x @ 3x
- 8. iOS Tesseract OCR Image Preperation
- 9. ionic&cordovaクロッピング画像
- 10. Upload Image Web Api
- 11. Bing Image Search API
- 12. いくつかのImage/GD問題:クロッピング、色と透明度の設定
- 13. iOS用Web API
- 14. iOS用のAPI
- 15. GWT画像クロッピング
- 16. HTML動的クロッピング
- 17. 動的テンソルアライメント/クロッピング
- 18. Bing Image Search APIリクエスト(Ajaxを使用)
- 19. Clarifai API for Local Image
- 20. Facebook Graph API、usepic image
- 21. jqueryを使用した画像のクロッピング
- 22. iBook Image iOS上でのカールアニメーション
- 23. ネイティブIOS APIを使用したIOSファイルサイズ
- 24. Emotion API Project Oxford base64 image
- 25. Magento(Android/IOS)用モバイルアプリApi
- 26. iOSアプリケーション用のVNCサーバーAPI
- 27. IOS CoreBluetooth API誤用近所
- 28. GoogleドライブiOS用リアルタイムAPI
- 29. CKFinder ciustomizationをクロッピングする
- 30. 画像Richfacesでのクロッピング
ブリリアント! iOS 7の風のように動作します。ありがとう! –