<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
DatePicker.backgroundColor=[UIColor whiteColor];
NSString * pathStr = [[self documentFolderPath]stringByAppendingString:@"/handfaceImage.png"];
NSLog(@"str = %@",pathStr);
handFaceImgView.image=[UIImage imageNamed:pathStr];
}
#pragma UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
if ((picker.sourceType==UIImagePickerControllerSourceTypeCamera)) {
NSLog(@"dic = %@",info);
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
UIImage * newImage = [SHOPUserRegisterInfo imageWithImageSimple:image scaledToSize:CGSizeMake(120, 120)];
handFaceImgView.image=newImage;
[self saveImage:newImage WithName:@"handfaceImage.png"];
[picker dismissViewControllerAnimated:YES completion:nil];
}
//裁剪照片尺寸
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
#pragma mark 保存图片到document
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
NSData* imageData = UIImagePNGRepresentation(tempImage);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];
}
#pragma mark 从文档目录下获取Documents路径
- (NSString *)documentFolderPath
{
return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}
//判断文件是否存在
NSString * pathStr = [[self documentFolderPath]stringByAppendingString:@"/handfaceImage.png"];
NSLog(@"str = %@",pathStr);
NSFileManager * fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:pathStr]) {
NSLog(@"存在头像");
}else{
NSLog(@"不存在头像");
}
#pragma UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
if ((picker.sourceType==UIImagePickerControllerSourceTypeCamera)) {
NSLog(@"dic = %@",info);
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
UIImage * newImage = [SHOPUserRegisterInfo imageWithImageSimple:image scaledToSize:CGSizeMake(120, 120)];
handFaceImgView.image=newImage;
[self saveImage:newImage WithName:@"handfaceImage.png"];
[picker dismissViewControllerAnimated:YES completion:nil];
}
//以下为另一个版本
/ 改变图像的尺寸,方便上传服务器
- (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;
}
上传头像
最新推荐文章于 2023-09-05 14:12:34 发布