Use the following code to implement a circular photo frame:
[Plain]
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];
Cgimagerelbid (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];
Cgimagerelbid (imageMasked );
[ImageView setImage: newImage];
UIGraphicsEndImageContext ();
When you set size --, the circular area is reduced, but when you set size ++, there is no response.
I don't know. Why?
Solution:
You can modify your code to this.
[Plain]
-(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];
Cgimagerelbid (imageMasked );
[_ ImageView setImage: newImage];
UIGraphicsEndImageContext ();
}