2011-06-30 142 views

回答

108
#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]; 
+6

You are missing'[imageLayer setMasksToBounds:YES];':) –

+0

是的,你必須使用Andres的建議將圖像剪切成ImageView的形狀 – Shailesh

+18

只要圖像是方形的,你就可以得到通過以半角寬度作爲角半徑的完美圓: [imageView.layer setCornerRadius:imageView.frame.size.width/2]; self.imageView.layer.masksToBounds = YES; –

0

退房CGImageCreateWithMask。創建一個橢圓形的面具,然後將其應用於圖像。

+0

怎麼橢圓形上分開我圖片?請幫我 – ram

0

你應該通過增加半徑它會變得更加全面,能夠參考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]; 
+0

,但這段代碼給出了矩形輸出... – ram

+0

是的,你必須使「矩形」作爲一個橢圓形...閱讀我的評論(「橢圓邏輯走到這裏」) – Maulik

+0

(「橢圓邏輯走到這裏」 ) - 這個鏈接不起作用。請幫助我maulik – ram

3

後長搜索我找到了正確的方式來圈圖像

從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]; 
35

圓我開始尋找到這幾個星期回來。我在這裏嘗試了所有的建議,但都沒有效果。在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]; 

} 
2

如果你只需要一個完美的圓,改變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; 
+0

這個動態的答案比選擇的答案要好得多。 –

3

爲了使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 
9

要具有的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 
+0

這對我有用。 –

+0

我可以使用uiview而不是uiimage使它變成橢圓形嗎? –

+0

如何爲目標c裁剪橢圓形圖像? –

3

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 
0

SWIFT 3答案來自@Mohammad薩迪克

let path = UIBezierPath.init(ovalIn: workingImgaeView.bounds) 
let maskLayer = CAShapeLayer(layer: layer) 
maskLayer.path = path.cgPath 
workingImgaeView.layer.mask = maskLayer