Circle Photo Frame effect in iphone

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.