iOS 实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像

//

//  ViewController.h

//  TestImageController

//

//  Created by huweibin on 15-5-17.

//  Copyright (c) 2015年 taogu. All rights reserved.

//

#import <UIKit/UIKit.h>

#import <MobileCoreServices/MobileCoreServices.h>

#import <QuartzCore/QuartzCore.h>

@interface ViewController :
UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

@end

//

//  ViewController.m

//  TestImageController

//

//

//  Created by huweibin on 15-5-17.

//  Copyright (c) 2015年 taogu. All rights reserved.

//

#import "ViewController.h"

@interface
ViewController ()

@property(nonatomic,
strong) UIImageView *img;

@property(nonatomic,
strong) NSData *fileData;

@end

@implementation ViewController

- (void)viewDidLoad

{

[super
viewDidLoad];

//设置图片框

self.img = [[UIImageView
alloc] initWithFrame:CGRectMake(100,
100,
93, 93)];

UIBarButtonItem *left = [[UIBarButtonItem
alloc] initWithTitle:@"picture"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(takePictureClick:)];

self.navigationItem.rightBarButtonItem = left;

[self.view
addSubview:self.img];

//图片路径

NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);

NSString *documentsDirectory = [paths
objectAtIndex:0];

NSString *imageFilePath = [documentsDirectory
stringByAppendingPathComponent:@"selfPhoto.jpg"];

NSLog(@"imageFile->>%@",imageFilePath);

UIImage *selfPhoto = [UIImage
imageWithContentsOfFile:imageFilePath];//

self.img.image = selfPhoto;

[self.img.layer
setCornerRadius:CGRectGetHeight([self.img
bounds]) / 2];

self.img.layer.masksToBounds
= YES;

}

//从相册获取图片

-(void)takePictureClick:(UIButton *)sender

{

UIActionSheet* actionSheet = [[UIActionSheet
alloc]

initWithTitle:@"请选择文件来源"

delegate:self

cancelButtonTitle:@"取消"

destructiveButtonTitle:nil

otherButtonTitles:@"照相机",@"摄像机",@"本地相簿",@"本地视频",nil];

[actionSheet
showInView:self.view];

}

#pragma mark -

#pragma UIActionSheet Delegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

NSLog(@"buttonIndex = [%ld]",(long)buttonIndex);

switch (buttonIndex) {

case 0://照相机

{

UIImagePickerController *imagePicker = [[UIImagePickerController
alloc]
init];

imagePicker.delegate =
self;

imagePicker.allowsEditing =
YES;

imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;

//            [self presentModalViewController:imagePicker animated:YES];

[self
presentViewController:imagePicker animated:YES
completion:nil];

}

break;

case 1://摄像机

{

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://本地相簿

{

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;

case 3://本地视频

{

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 didFinishPickingMediaWithInfo:(NSDictionary
*)info

{

if ([[info
objectForKey:UIImagePickerControllerMediaType]
isEqualToString:(__bridge
NSString *)kUTTypeImage]) {

UIImage *img = [info
objectForKey:UIImagePickerControllerEditedImage];

[self
performSelector:@selector(saveImage:) 
withObject:img afterDelay:0.5];

}

else
if ([[info
objectForKey:UIImagePickerControllerMediaType]
isEqualToString:(__bridge
NSString *)kUTTypeMovie]) {

NSString *videoPath = [[info
objectForKey:UIImagePickerControllerMediaURL]
path];

self.fileData = [NSData
dataWithContentsOfFile:videoPath];

}

//    [picker dismissModalViewControllerAnimated:YES];

[picker dismissViewControllerAnimated:YES
completion:nil];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

//    [picker dismissModalViewControllerAnimated:YES];

[picker dismissViewControllerAnimated:YES
completion:nil];

}

- (void)saveImage:(UIImage *)image {

//    NSLog(@"保存头像!");

//    [userPhotoButton setImage:image forState:UIControlStateNormal];

BOOL success;

NSFileManager *fileManager = [NSFileManager
defaultManager];

NSError *error;

NSArray *paths =
NSSearchPathForDirectoriesInDomains(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(80.0f, 80.0f)];//将图片尺寸改为80*80

UIImage *smallImage = [self
thumbnailWithImageWithoutScale:image
size:CGSizeMake(93,
93)];

[UIImageJPEGRepresentation(smallImage,
1.0f)
writeToFile:imageFilePath atomically:YES];//写入文件

UIImage *selfPhoto = [UIImage
imageWithContentsOfFile:imageFilePath];//读取图片文件

self.img.image = selfPhoto;

}

// 改变图像的尺寸,方便上传服务器

- (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.保持原来的长宽比,生成一个缩略图

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

}

UIGraphicsBeginImageContext(asize);

CGContextRef context =
UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [[UIColor
clearColor] CGColor]);

UIRectFill(CGRectMake(0,
0, asize.width, asize.height));//clear background

[image
drawInRect:rect];

newimage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

return newimage;

}

@end

时间: 2024-10-04 17:11:02

iOS 实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像的相关文章

iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像

//弹出actionsheet.选择获取头像的方式//从相册获取图片 -(void)takePictureClick:(UIButton *)sender { // /*注:使用,需要实现以下协议:UIImagePickerControllerDelegate, // UINavigationControllerDelegate // */ // UIImagePickerController *picker = [[UIImagePickerController alloc]init]; //

生成圆形头像 (附效果图)

效果图 代码可复制 #import "ViewController.h" @interface ViewController () @property (nonatomic,strong)UIImageView *Headimage; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self creatUI]; } -(void)creatUI { _Headimage =

android分析之自定义圆形头像

package de.hdodenhof.circleimageview; public class CircleImageView extends ImageView { private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;//决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等 private static final Bitmap.Config BITMAP_CONFIG =

【Android】自定义圆形ImageView(圆形头像 可指定大小)

最近在仿手Q的UI,这里面经常要用到的就是圆形头像,看到 在android中画圆形图片的几种办法 这篇文章,了解了制作这种头像的原理.不过里面提供的方法还有一个不足的地方就是不能根据实际需求改变图片的大小,也就是说提供的原图是大尺寸的,转换之后的图片也是大尺寸的,这显然不符合我们实际项目中的需求.于是我对里面介绍的第一种方法做了一番改进,使其能直接在XML中指定图片的大小. 大体步骤 将原图居中裁剪成正方形 根据指定的宽度对正方形进行缩放 裁剪成圆形 效果 代码实现 package com.de

自定义控件 编辑和选取圆形头像

android大家都有很多需要用户上传头像的需求,有的是选方形,有的是圆角矩形,有的是圆形. 首先我们要做一个处理图片的自定义控件,把传入的图片,经过用户选择区域,处理成一定的形状. 有的app是通过在图片上画一个矩形区域表示选中的内容,有的则是通过双指放大缩小,拖动图片来选取图片.圆形头像,还是改变图片比较好 圆形区域可调节大小. 这个自定义View的图像部分分为三个,背景图片,半透明蒙层,和亮色区域--还是直接贴代码得了 package com.example.jjj.widget; imp

【Android】自己定义圆形ImageView(圆形头像 可指定大小)

近期在仿手Q的UI,这里面常常要用到的就是圆形头像,看到 在android中画圆形图片的几种办法 这篇文章,了解了制作这样的头像的原理.只是里面提供的方法另一个不足的地方就是不能依据实际需求改变图片的大小,也就是说提供的原图是大尺寸的,转换之后的图片也是大尺寸的,这显然不符合我们实际项目中的需求.于是我对里面介绍的第一种方法做了一番改进,使其能直接在XML中指定图片的大小. 大体步骤 将原图居中裁剪成正方形 依据指定的宽度对正方形进行缩放 裁剪成圆形 效果 代码实现 package com.de

iOS 画圆形头像

demo下载地址:http://pan.baidu.com/s/1mgBf6YG _logoImageView.image = [self getEllipseImageWithImage:[UIImage imageNamed:@"1_wsq724439564.jpg"]]; -(UIImage*)getEllipseImageWithImage:(UIImage*)originImage { CGFloat padding = 5;//圆形图像距离图像的边距 UIColor* ep

Duilib实现圆形头像控件

.h文件 1 #ifndef __UIHEADICON_H__ 2 #define __UIHEADICON_H__ 3 4 5 /* 6 名称:圆形头像控件(派生CButtonUI类) 7 */ 8 9 10 class CHeadUI: public CButtonUI 11 { 12 public: 13 14 CHeadUI(); 15 16 LPCTSTR GetClass() const; 17 LPVOID GetInterface(LPCTSTR pstrName); 18 19

Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码

Android项目开发中经常会遇见需要实现圆角或者圆形的图片功能,如果仅仅使用系统自带的ImageView控件显然无法实现此功能,所以通过系列文章的形式由简到繁全方位的介绍一下此功能的实现,巩固一下自身的学习,同时,和广大网友交流分享 本项目源码下载地址:链接:http://pan.baidu.com/s/1sljdvtF 密码:xj85 首先看效果图 首先看一下CircleImageView的主要流程 1. 首先通过setImageXxx()方法设置图片Bitmap: 2. 进入构造函数Cir