// Pop up the actionsheet. Select how to get the Avatar
// Obtain the image from the album-(void) takePictureClick :( UIButton *) sender {// * Note: to use it, you must implement the following protocol: UIImagePickerControllerDelegate, // UINavigationControllerDelegate // * // UIImagePickerController * picker = [[UIImagePickerController alloc] init]; // sets the image source (album) // picker. sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; // set the proxy // picker. delegate = self; // You can edit the settings. // picker. allowsEditing = YES; // open the picker interface // [self p ResentViewController: picker animated: YES completion: nil]; UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: @ "select the file source" delegate: self cancelButtonTitle: @ "cancel" failed: nil otherButtonTitles: @ "camera", @ "camera", @ "Local album", @ "local video", nil]; [actionSheet showInView: self. view] ;}# pragma mark-# pragma UIActionSheet Delegate-(void) actionSheet :( UIActionSheet *) actionSheet clickedButtonAtI Ndex :( NSInteger) buttonIndex {NSLog (@ "buttonIndex = [% d]", buttonIndex); switch (buttonIndex) {case 0: // camera {UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init]; imagePicker. delegate = self; imagePicker. allowsEditing = YES; imagePicker. sourceType = UIImagePickerControllerSourceTypeCamera; // [self presentModalViewController: imagePicker animated: YES]; [self presentViewContr Oller: imagePicker animated: YES completion: nil];} break; case 1: // camera {UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init]; imagePicker. delegate = self; imagePicker. allowsEditing = YES; imagePicker. sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker. videoQuality = UIImagePickerControllerQualityTypeLow; // [self presentModalViewController: imagePicker animated: YES]; [self presentViewController: imagePicker animated: YES completion: nil];} break; case 2: // local album {UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init]; imagePicker. delegate = self; imagePicker. allowsEditing = YES; imagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // [self presentModalViewController: imagePicker animated: YES]; [self presentViewContro Roller: imagePicker animated: YES completion: nil];} break; case 3: // local video {UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init]; imagePicker. delegate = self; imagePicker. allowsEditing = YES; imagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // [self presentModalViewController: imagePicker animated: YES]; [self presentViewController: imagePicker animated: YES Completion: nil];} break; default: break; }}# pragma mark-# pragma UIImagePickerController Delegate-(void) imagePickerController :( UIImagePickerController *) picker syntax :( NSDictionary *) info {if ([[info objectForKey: UIImagePickerControllerMediaType] is%tostring :( _ bridge NSString *) kUTTypeImage]) {UIImage * img = [info objectForKey: UIImagePickerControllerEditedImage]; [se Lf parameter mselector: @ selector (saveImage :) withObject: img afterDelay: 0.5];} else if ([[info objectForKey: Encrypted] isw.tostring :( _ bridge NSString *) kUTTypeMovie]) {NSString * videoPath = [[info objectForKey: UIImagePickerControllerMediaURL] path]; self. fileData = [NSData dataWithContentsOfFile: videoPath];} // [picker dismissModalViewControllerAnimated: YES]; [picker dismis SViewControllerAnimated: YES completion: nil];}-(void) Restart :( UIImagePickerController *) picker {// [picker restart: YES]; [picker dismissViewControllerAnimated: YES completion: nil];} -(void) saveImage :( UIImage *) image {// NSLog (@ "Save the Avatar! "); // [UserPhotoButton setImage: image forState: UIControlStateNormal]; BOOL success; NSFileManager * fileManager = [NSFileManager defaultManager]; NSError * error; NSArray * paths = NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; NSString * imageFilePath = [documentsDirectory stringByAppendingPathComponent: @ "selfPhoto.jpg"]; NSLog (@ "imageFile-> % @", imageFilePath); success = [fileManager fileExistsAtPath: imageFilePath]; if (success) {success = [fileManager removeItemAtPath: imageFilePath error: & error];} // UIImage * smallImage = [self scaleFromImage: image toSize: CGSizeMake (80366f, 80366f)]; // change the image size to 80*80 UIImage * smallImage = [self size: image size: CGSizeMake (93, 93)]; [UIImageJPEGRepresentation (smallImage, 1.0f) writeToFile: imageFilePath atomically: YES]; // write the file UIImage * selfPhoto = [UIImage imageWithContentsOfFile: imageFilePath]; // read the image file // [userPhotoButton setImage: selfPhoto forState: UIControlStateNormal]; self. img. image = selfPhoto;} // modify the image size to facilitate uploading to the server-(UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size {UIGraphicsBeginImageContext (size ); [image drawInRect: CGRectMake (0, 0, size. width, size. height)]; UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return newImage ;}
2. Maintain the aspect ratio of the original image and generate an image of the desired size.
// 2. maintain the original aspect ratio and generate a thumbnail-(UIImage *) thumbnailWithImageWithoutScale :( UIImage *) image size :( CGSize) asize {UIImage * newimage; if (nil = image) {newimage = nil;} else {CGSize oldsize = image. size; CGRect rect; if (asize. width/asize. height> oldsize. width/oldsize. height) {rect. size. width = asize. height * oldsize. width/oldsize. height; rect. size. height = asize. height; rect. origin. x = (asize. width-rect. size. width)/2; rect. origin. y = 0;} else {rect. size. width = asize. width; rect. size. height = asize. width * oldsize. height/oldsize. width; rect. origin. x = 0; rect. origin. y = (asize. height-rect. size. height)/2;} convert (asize); CGContextRef context = UIGraphicsGetCurrentContext (); convert (context, [[UIColor clearColor] CGColor]); UIRectFill (CGRectMake (0, 0, 0, asize. width, asize. height); // clear background [image drawInRect: rect]; newimage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext ();} return newimage ;}
3. Show the circular Avatar
NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; NSString * imageFilePath = [documentsDirectory failed: @ "selfPhoto.jpg"]; NSLog (@ "imageFile-> % @", imageFilePath); UIImage * selfPhoto = [UIImage imageWithContentsOfFile: imageFilePath]; // self. img. image = selfPhoto; [self. img. layer setCornerRadius: CGRectGetHeight ([self. img bounds])/2]; // modify the radius to realize the circular self of the Avatar. img. layer. masksToBounds = YES;
We hope to help you, if you have a better implementation method. Please tell me.