請提供有關如何在橢圓形或圓形上裁剪UIImage
的想法。請指導我。如何裁剪橢圓形或圓形UIImage?
回答
#import <QuartzCore/QuartzCore.h>
CALayer *imageLayer = YourImageview.layer;
[imageLayer setCornerRadius:5];
[imageLayer setBorderWidth:1];
[imageLayer setMasksToBounds:YES];
。
只要圖像是一個正方形,你可以通過採取一半的寬度拐角半徑的正圓:
[imageView.layer setCornerRadius:imageView.frame.size.width/2];
您還需要添加
[imageView.layer setMasksToBounds:YES];
你應該通過增加半徑它會變得更加全面,能夠參考This ...
// Create the image from a png file
UIImage *image = [UIImage imageNamed:@"prgBinary.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// Get size of current image
CGSize size = [image size];
// Frame location in view to show original image
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];
// Create rectangle that represents a cropped image
// from the middle of the existing image
CGRect rect = CGRectMake(size.width/4, size.height/4 ,
(size.width/2), (size.height/2)); //oval logic goes here
// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(0, 200, (size.width/2), (size.height/2))];
[[self view] addSubview:imageView];
[imageView release];
後長搜索我找到了正確的方式來圈圖像
從URL下載支持存檔文件http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
用來調整圖像和轉換成0#import "UIImage+RoundedCorner.h"
#import "UIImage+Resize.h"
以下幾行半徑
UIImage *mask = [UIImage imageNamed:@"mask.jpg"];
mask = [mask resizedImage:CGSizeMake(47, 47) interpolationQuality:kCGInterpolationHigh ];
mask = [mask roundedCornerImage:23.5 borderSize:1];
圓我開始尋找到這幾個星期回來。我在這裏嘗試了所有的建議,但都沒有效果。在RTFM的偉大傳統中,我去閱讀了蘋果關於Quartz 2D Programming的文檔,並提出了這個問題。請嘗試一下,讓我知道你的方式。
的代碼可以相當容易地改變,以裁剪到elipse,或由路徑定義的任何其他形狀。
請務必包括石英2D在您的項目。
#include <math.h>
+ (UIImage*)circularScaleAndCropImage:(UIImage*)image frame:(CGRect)frame {
// This function returns a newImage, based on image, that has been:
// - scaled to fit in (CGRect) rect
// - and cropped within a circle of radius: rectWidth/2
//Create the bitmap graphics context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(frame.size.width, frame.size.height), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
//Get the width and heights
CGFloat imageWidth = image.size.width;
CGFloat imageHeight = image.size.height;
CGFloat rectWidth = frame.size.width;
CGFloat rectHeight = frame.size.height;
//Calculate the scale factor
CGFloat scaleFactorX = rectWidth/imageWidth;
CGFloat scaleFactorY = rectHeight/imageHeight;
//Calculate the centre of the circle
CGFloat imageCentreX = rectWidth/2;
CGFloat imageCentreY = rectHeight/2;
// Create and CLIP to a CIRCULAR Path
// (This could be replaced with any closed path if you want a different shaped clip)
CGFloat radius = rectWidth/2;
CGContextBeginPath (context);
CGContextAddArc (context, imageCentreX, imageCentreY, radius, 0, 2*M_PI, 0);
CGContextClosePath (context);
CGContextClip (context);
//Set the SCALE factor for the graphics context
//All future draw calls will be scaled by this factor
CGContextScaleCTM (context, scaleFactorX, scaleFactorY);
// Draw the IMAGE
CGRect myRect = CGRectMake(0, 0, imageWidth, imageHeight);
[image drawInRect:myRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
在您的UIView類中包含以下代碼,用您自己的圖像名稱替換「monk2.png」。
- (void)drawRect:(CGRect)rect {
UIImage *originalImage = [UIImage imageNamed:[NSString stringWithFormat:@"monk2.png"]];
CGFloat oImageWidth = originalImage.size.width;
CGFloat oImageHeight = originalImage.size.height;
// Draw the original image at the origin
CGRect oRect = CGRectMake(0, 0, oImageWidth, oImageHeight);
[originalImage drawInRect:oRect];
// Set the newRect to half the size of the original image
CGRect newRect = CGRectMake(0, 0, oImageWidth/2, oImageHeight/2);
UIImage *newImage = [self circularScaleAndCropImage:originalImage frame:newRect];
CGFloat nImageWidth = newImage.size.width;
CGFloat nImageHeight = newImage.size.height;
//Draw the scaled and cropped image
CGRect thisRect = CGRectMake(oImageWidth+10, 0, nImageWidth, nImageHeight);
[newImage drawInRect:thisRect];
}
如果你只需要一個完美的圓,改變UIImageView的形狀可能會有所幫助。 只需將QuartzCore框架添加到您的項目,並添加幾行代碼生命週期中的某處顯示ImageView的面前:
#import <QuartzCore/QuartzCore.h>
.
.
.
//to crop your UIImageView to show only a circle
yourImageView.layer.cornerRadius = yourImageView.frame.size.width/2;
yourImageView.clipsToBounds = YES;
這個動態的答案比選擇的答案要好得多。 –
爲了使RoundShape圖片
第一步:在.h文件中
@property (strong, nonatomic) IBOutlet UIImageView *songImage;
第二步:英寸米文件
- (void)viewDidLoad
{
self.songImage.layer.cornerRadius = self.songImage.frame.size.width/2;
self.songImage.clipsToBounds = YES;
//To give the Border and Border color of imageview
self.songImage.layer.borderWidth = 1.0f;
self.songImage.layer.borderColor = [UIColor colorWithRed:249/255.0f green:117/255.0f blue:44/255.0f alpha:1.0f].CGColor;
}
或夫特
cell.songImage.layer.cornerRadius = cell.songImage.frame.size.width/2;
cell.songImage.clipsToBounds = true
//To give the Border and Border color of imageview
cell.songImage.layer.borderWidth = 1.0
cell.songImage.layer.borderColor = UIColor(red: 50.0/255, green: 150.0/255, blue: 65.0/255, alpha: 1.0).CGColor
要具有的ImageView在橢圓形狀並不困難。
你可以做以下
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = path.CGPath;
yourImage.layer.mask = maskLayer;
如果傳遞給bezierPathWithOvalInRect矩形是方形的圖像將被裁剪成圈。
Swift代碼
let path = UIBezierPath(ovalIn: yourImageView.bounds)
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
yourImage.layer.mask = maskLayer
這對我有用。 –
我可以使用uiview而不是uiimage使它變成橢圓形嗎? –
如何爲目標c裁剪橢圓形圖像? –
SWIFT
var vwImage = UIImageView(image: UIImage(named: "Btn_PinIt_Normal.png"))
vwImage.frame = CGRectMake(0, 0, 100, 100)
vwImage.layer.cornerRadius = vwImage.frame.size.width/2
SWIFT 3答案來自@Mohammad薩迪克
let path = UIBezierPath.init(ovalIn: workingImgaeView.bounds)
let maskLayer = CAShapeLayer(layer: layer)
maskLayer.path = path.cgPath
workingImgaeView.layer.mask = maskLayer
- 1. 從原始UIImage中裁剪圓形或橢圓形圖像
- 2. Android位圖裁剪橢圓形
- 3. Pyqtgraph:如何繪製橢圓或圓形
- 4. 如何在Android中裁剪橢圓形或面部形狀的圖像?
- 5. 圓形裁剪圖像
- 6. SVG剪裁橢圓BUG
- 7. 如何裁剪圓形圖像openvc android
- 8. 如何將圖像裁剪成圓形?
- 9. CSS圓形裁剪矩形圖像
- 10. 在矩形內創建橢圓/圓形
- 11. 彎曲線形成圓形和橢圓
- 12. CSS或jQuery/JavaScript橢圓形/圓形方形進度條
- 13. 從圖像裁剪橢圓形而不是在OpenCV中繪製橢圓形表格
- 14. CSS div橢圓形
- 15. imagemagick中的圖像圓形裁剪
- 16. 將圖像裁剪成圓形
- 17. 用UIImagePickerController iOS的圓形裁剪相機?
- 18. 如何在Android中的矩形內裁剪圓形路徑
- 19. 如何製作圓形的橢圓形按鈕?
- 20. 如何繪製手繪的橢圓或圓形?
- 21. 如何檢查橢圓形是否觸及任何其他橢圓形VB
- 22. OpenCV裁剪圖像與橢圓
- 23. 如何在Javascript中使用Photoshop創建圓形或橢圓形選區?
- 24. OpenGL圓形繪圖變橢圓
- 25. 用橢圓繪製圓形pygame
- 26. 沿着圓形的Qt動畫橢圓
- 27. OpenGL繪製橢圓而不是圓形
- 28. 如何繪製一些形狀(橢圓形或橢圓形)並計算其面積?
- 29. 如何在jsplumb流程圖上添加菱形或橢圓形?
- 30. CSS定義形狀橢圓
You are missing'[imageLayer setMasksToBounds:YES];':) –
是的,你必須使用Andres的建議將圖像剪切成ImageView的形狀 – Shailesh
只要圖像是方形的,你就可以得到通過以半角寬度作爲角半徑的完美圓: [imageView.layer setCornerRadius:imageView.frame.size.width/2]; self.imageView.layer.masksToBounds = YES; –