你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> iphone中的圓形相框效果

iphone中的圓形相框效果

編輯:關於IOS

想用下面的代碼實現一個圓形相框:

CGContextRef context = CGBitmapContextCreate(NULL, self.bounds.size.width, self.bounds.size.height, 8, 4 * self.bounds.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst); 
CGContextAddArc(context, 1024/2, 768/2, size, 0, 6.3, 0); 
CGContextClosePath(context); 
CGContextClip(context); 
CGContextDrawImage(context, self.bounds, imageView.image.CGImage); 
CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
CGContextRelease(context); 
UIImage *newImage = [UIImage imageWithCGImage:imageMasked]; 
CGImageRelease(imageMasked); 
 
[imageView setImage:newImage]; 
UIGraphicsEndImageContext(); 

CGContextRef context = CGBitmapContextCreate(NULL, self.bounds.size.width, self.bounds.size.height, 8, 4 * self.bounds.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst);
CGContextAddArc(context, 1024/2, 768/2, size, 0, 6.3, 0);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, self.bounds, imageView.image.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
CGImageRelease(imageMasked);

[imageView setImage:newImage];
UIGraphicsEndImageContext();

在設置size--,圓形區域就會縮小,但是設置size++的時候,就沒有反應。
不知道怎麼呢?

iphone中的圓形相框效果

解決方案:

你的代碼修改成這樣就可以了

- (IBAction)minus:(id)sender {
self.radius = @([self.radius floatValue]+10.0f);
[self redrawImage];
}

- (IBAction)plus:(id)sender {
self.radius = @([self.radius floatValue]-10.0f);
[self redrawImage];
}

-(void)redrawImage
{
//set the original image first
[_imageView setImage:[UIImage imageNamed:@"your image goes here"]];

CGFloat size = [self.radius floatValue];
CGContextRef context = CGBitmapContextCreate(NULL, self.view.bounds.size.width, self.view.bounds.size.height, 8, 4 * self.view.bounds.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst);
CGContextAddArc(context, 320/2, 460/2, size, 0, 2*M_PI, 0);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, self.view.bounds, _imageView.image.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
CGImageRelease(imageMasked);

[_imageView setImage:newImage];
UIGraphicsEndImageContext();
}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved