IOS cuts round images and displays (similar to WeChat avatar)

This article mainly explain how to choose a picture from the photos after the cut into round head and display, similar to the WeChat picture that model.

This method is also applicable to image capturing method was similar, so no longer.

This paper is mainly used in the iOS 10 environment, at this time if you want to use the use of photos, camera system and other functions need authorization, authorization methods are as follows:

Click on the right button project directory “Info.plist – Open – As files > > Source Code, open the replication following you use in the application of privacy permissions (describe their changes):

< key> NSVideoSubscriberAccountUsageDescription< /key> < string> < /string> < key> NSBluetoothPeripheralUsageDescription< /key> < string> /string> Bluetooth permissions < < key> NSSpeechRecognitionUsageDescription< /key> < string> /string> speech recognition authority < < key> NSSiriUsageDescription< /key> < string> Siri; /string> < power < key> NSRemindersUsageDescription< /key> < string> < /string> < key> NSPhotoLibraryUsageDescription< /key> < string> /string> photo album permissions < < key> kTCCServiceMediaLibrary< /key> < string> < /string> < key> NSMotionUsageDescription< /key> < string> /string> motion permissions < < key> NSMicrophoneUsageDescription< /key> < string> /string> microphone permissions < < key> NSAppleMusicUsageDescription< /key> < string> /string> Music Rights & lt; < key> NSLocationWhenInUseUsageDescription< /key> < string> location permissions < /string> < key> NSLocationUsageDescription< /key> < string> location permissions < /string> < key> NSLocationAlwaysUsageDescription< /key> < string> location permissions < /string> < key> NSHomeKitUsageDescription< /key> < string> < /string> < key> NSHealthUpdateUsageDescription< /key> < string> The health authority < /string> < key> NSHealthShareUsageDescription< /key> < string> /string> health permissions < < key> NSContactsUsageDescription< /key> < string> mail list permissions < /string> key> NSCameraUsageDescription< lt; /key> < string> /string> camera permissions < < key> NSCalendarsUsageDescription< /key> < string> calendar permissions < /string>

Below, officially entered this paper to realize the function of the code.

1. use Xcode’s storyboard to create a button and a imageView

After creating the effect as shown in figure 1. The size of the imageView affects the size of the final display. Please set it according to the actual situation.

IOS cuts round images and displays (similar to WeChat avatar),
, figure 1

2. create a UIImage category (Category)

Creates a new file, select “Objective-C File”, as shown in figure 2:

IOS cuts round images and displays (similar to WeChat avatar),
, figure 2

In the pop-up dialog box shown in Figure 3, “File” writes the name of the class (in this case, DY), “File Type”, “Category”, “Class”, and “UIImage”. Then click the “Next” button to save the new file.

IOS cuts round images and displays (similar to WeChat avatar),
, figure 3

3. write code in the category

In the UIImage+DY.h file

#import < UIKit/UIKit.h> @interface, UIImage (DY) + (instancetype), circleOldImage: (UIImage *), originalImage, borderWidth: (CGFloat), borderWidth, borderColor: (UIColor *) borderColor, @end

In the UIImage+DY.m file

#import "UIImage+DY.h" @implementation UIImage (DY) + (instancetype) circleOldImage: (UIImage *) originalImage borderWidth: (CGFloat) borderWidth borderColor: (UIColor * borderColor) {/ / 1. *oldImage loading original UIImage = originalImage; imageW = 2. / / open context CGFloat oldImage.size.width + 2 * borderWidth; CGFloat imageH = oldImage.size.height + 2 * borderWidth; CGSize imageSize = CGSizeMake (imageW, imageH); UIGraphicsBeginImageContextWithOptions (imageSize, NO, 0); / / 3. to obtain the current context CGContextRef (CTX = UIGraphicsGetCurrentContext); / / 4. (circle) [borderColor set] picture frame; CGFloat bigRadius = imageW * 0.5; / / circle radius CGFloat centerX = bigRadius; / / CGFlo. At centerY = bigRadius; CGContextAddArc (CTX, centerX, centerY, bigRadius, M_PI * 2, 0, 0); CGContextFillPath (CTX); / / CGFloat / / 5. small circle smallRadius = bigRadius - borderWidth; CGContextAddArc (CTX, centerX, centerY, smallRadius, M_PI * 2, 0, 0); / / (cutting things will influence painting by cutting back) CGContextClip (CTX); / / [oldImage drawInRect:CGRectMake 6. drawing (borderWidth, borderWidth, oldImage.size.width, oldImage.size.height)]; / / 7. = UIGraphicsGetImageFromCurrentImageContext (*newImage) UIImage; / / the 8. end context UIGraphicsEndImageContext (); return newImage @end;}

(instancetype) + circleOldImage: (UIImage) originalImage borderWidth: (CGFloat) borderWidth borderColor: (UIColor) borderColor method: This is a kind of method, the final return is a class of UIImage; originalImage parameter method is from the photos or pictures selected photos (possibly after clipping system) borderWidth; refers to the parameters in the method is the final display circular image border width, can according to their own needs to set the width; borderColor parameter method refers to the final round of the image shows the color of the border, you can set the color according to their own needs.

4., cut into a circular image and display

ViewController.h file

#import < UIKit/UIKit.h> #import "UIImage+DY.h" / / @interface: UIViewController< ViewController class loading UINavigationControllerDelegate, UIImagePickerControllerDelegate> / / be sure to add the two Delegate @property (strong, nonatomic) UIImagePickerController *imagePickerController; (IBAction) - btnPressed: (ID) sender; @property (strong, nonatomic) IBOutlet UIImageView *ablumImageView; @end

ViewController.m file

#import "ViewController.h" @interface ViewController (@end @implementation ViewController) - (void) viewDidLoad {[super viewDidLoad]; Do any additional setup after loading / the view, typically from a (void) - nib.} didReceiveMemoryWarning {[super didReceiveMemoryWarning]; Dispose any resources that can / of be recreated.} - (IBAction) btnPressed: (ID) {sender if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {/ / first to determine whether to support the photo gallery, the method parameter in the _imagePickerController.sourceType and the value of keeping / / if _imagePickerController support = [[UIImagePickerController alloc]init]; _imagePickerContro Ller.view.backgroundColor [UIColor = orangeColor]; _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; _imagePickerController.delegate = self; _imagePickerController.allowsEditing = YES; / / the default parameter is NO, suggestion is set to YES, or cut into round pictures will get to the oval picture, and your [self presentViewController:_imagePickerController animated:YES completion:nil] expected to be quite different;}} - (void (imagePickerController:) UIImagePickerController picker didFinishPickingMediaWithInfo: (NSDictionary) * *) info [UIImage circleOldImage:[info objectForKey:UIImagePickerControllerEditedImage] borderWidth:30.0f {_ablumImageView.image = borderColor [UIColor: orangeColor]]; / / Info the value of Key in the "UIImagePickerControllerEditedImage" that is the choice after cutting the picture, if you use the Key value, _imagePickerController.allowsEditing value must be set to YES. _imagePickerController.allowsEditing / / if the value of the NO setting, the Key value should be set to UIImagePickerControllerOriginalImage / Info value of Key in the following: NSString *const UIImagePickerControllerMediaType; specify the user's choice of media types (finally extended) NSString *const UIImagePickerControllerOriginalImage NSString *const UIImagePickerControllerEditedImage; original image; modified pictures NSString *const UIImagePickerControllerCropRect cut; the size of NSString *const UIImagePickerControllerMediaURL; NSString *const UIImagePickerControllerReferenceURL media URL URL NSString *const UIImagePickerControllerMediaMetadata; original; when the data source is the camera when this value is valid * [self dismissViewControllerAnimated:YES completion:nil];} - (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker{[self dismissViewControllerAnimated:YES completion:nil] @end;}