2011-06-30 59 views

Trả lời

108
#import <QuartzCore/QuartzCore.h> 

CALayer *imageLayer = YourImageview.layer; 
     [imageLayer setCornerRadius:5]; 
     [imageLayer setBorderWidth:1]; 
     [imageLayer setMasksToBounds:YES]; 

bằng cách tăng bán kính nó sẽ trở nên tròn có thể nhiều hơn nữa.

Chừng nào hình ảnh là một hình vuông, bạn có thể có được một vòng tròn hoàn hảo bằng cách lấy một nửa chiều rộng như bán kính góc:

[imageView.layer setCornerRadius:imageView.frame.size.width/2]; 

Bạn cũng cần phải thêm

[imageView.layer setMasksToBounds:YES]; 
+6

Bạn đang thiếu '[imageLayer setMasksToBounds: YES];' :) –

+0

Có, Bạn phải sử dụng đề xuất của Andres để cắt hình ảnh vào hình dạng của ImageView – Shailesh

+18

Miễn là hình ảnh là hình vuông, bạn có thể nhận được vòng tròn hoàn hảo bằng cách lấy một nửa chiều rộng làm bán kính góc: [imageView.layer setCornerRadius: imageView.frame.size.width/2]; self.imageView.layer.masksToBounds = YES; –

0

Khám phá CGImageCreateWithMask. Tạo một mặt nạ của hình bầu dục của bạn, sau đó áp dụng nó vào hình ảnh.

+0

làm thế nào để tách hình dạng hình bầu dục trên của tôi hình ảnh? xin vui lòng giúp tôi – ram

0

bạn nên tham khảo 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

nhưng mã này cho đầu ra hình chữ nhật ... – ram

+0

yeah bạn phải thực hiện "rect" như một hình bầu dục ... đọc ý kiến ​​của tôi ("logic hình bầu dục đi đây") – Maulik

+0

("logic hình bầu dục đi đây") - liên kết này không hoạt động. pls giúp tôi maulik – ram

3

Sau một tìm kiếm dài Tôi đã tìm thấy cách chính xác để khoanh tròn hình ảnh

Tải xuống tệp lưu trữ Hỗ trợ từ URL http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

#import "UIImage+RoundedCorner.h" 
#import "UIImage+Resize.h" 

dòng sau sử dụng để thay đổi kích thước hình ảnh và chuyển đổi trong làm tròn với bán kính

UIImage *mask = [UIImage imageNamed:@"mask.jpg"]; 

mask = [mask resizedImage:CGSizeMake(47, 47) interpolationQuality:kCGInterpolationHigh ]; 
mask = [mask roundedCornerImage:23.5 borderSize:1]; 
35

tôi bắt đầu nhìn vào điều này một vài tuần trở lại. Tôi đã thử tất cả các đề xuất ở đây, không có đề xuất nào hoạt động tốt. Trong truyền thống tuyệt vời của RTFM, tôi đã đọc và đọc tài liệu của Apple theo số Quartz 2D Programming và đưa ra điều này. Hãy thử nó ra và cho tôi biết làm thế nào bạn đi.

Mã có thể dễ dàng thay đổi để cắt thành hình elip hoặc bất kỳ hình dạng nào khác được xác định bởi đường dẫn.

Đảm bảo bạn bao gồm Quartz 2D trong dự án của bạn.

#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; 
} 

Bao gồm mã sau trong lớp UIView thay thế "monk2.png" bằng tên hình ảnh của riêng bạn.

- (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

Nếu bạn chỉ cần vòng tròn hoàn hảo, việc thay đổi hình dạng của UIImageView có thể hữu ích. Chỉ cần thêm khuôn khổ QuartzCore để dự án của bạn và thêm các dòng mã ở đâu đó trong vòng đời trước IMAGExem được hiển thị:

#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

Câu trả lời năng động này là câu trả lời tốt hơn nhiều so với câu trả lời được chọn. –

3

Để thực hiện một hình ảnh RoundShape

Bước 1: trong file .h

@property (strong, nonatomic) IBOutlet UIImageView *songImage; 

Bước 2: in.m tập tin

- (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; 

} 

HOẶC Đối Swift

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

Để có IMAGExem hình oval không phải là khó khăn.

Bạn có thể làm như sau

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:yourImageView.bounds]; 
CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.path = path.CGPath; 
yourImage.layer.mask = maskLayer; 

Nếu rect truyền cho bezierPathWithOvalInRect là Quảng trường hình ảnh sẽ được cắt vào cộng đồng.

Swift Mã

let path = UIBezierPath(ovalIn: yourImageView.bounds) 
let maskLayer = CAShapeLayer() 
maskLayer.path = path.cgPath 
yourImage.layer.mask = maskLayer 
+0

tính năng này phù hợp với tôi. –

+0

tôi có thể sử dụng uiview thay vì uiimage để làm hình bầu dục không? –

+0

Làm thế nào để cắt hình dạng hình bầu dục cho mục tiêu 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 câu trả lời xuất phát từ @Mohammad Sadiq

let path = UIBezierPath.init(ovalIn: workingImgaeView.bounds) 
let maskLayer = CAShapeLayer(layer: layer) 
maskLayer.path = path.cgPath 
workingImgaeView.layer.mask = maskLayer 
Các vấn đề liên quan