标签:
//相机
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentViewController:picker animated:YES completion:nil];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//设置选择后的图片可被编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image, 0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
//关闭相册界面
[picker dismissViewControllerAnimated:YES completion:nil];
UIGraphicsBeginImageContext(self.headImageButton.frame.size);
[image drawInRect:CGRectMake(0, 0, self.headImageButton.frame.size.width, self.headImageButton.frame.size.height)];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
//创建一个选择后图片的小图标放在下方
//类似微薄选择图后的效果
self.smallimage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 120, 40, 40)];
self.smallimage.image = newImage;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = nil;
NSString *mimeType = nil;
imageData=UIImageJPEGRepresentation(self.smallimage.image, 1);
_picture=[[NSString alloc]init];
mimeType=[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
_picture= (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)mimeType,NULL,CFSTR(":/?#[]@!$&’()*+,;="),kCFStringEncodingUTF8);
NSUserDefaults *userDef=[NSUserDefaults standardUserDefaults];
// NSDictionary *bdeditInfo=[NSDictionary dictionaryWithObjectsAndKeys:_picture,@"gd_picture",nil];
[userDef setObject:_picture forKey:@"gd_picture"];
dispatch_async(dispatch_get_main_queue(), ^{
});
});
//加在视图中
// [self.headImageButton setImage:smallimage.image forState:UIControlStateNormal];
[self.headImageButton setBackgroundImage:self.smallimage.image forState:UIControlStateNormal];
self.headImageButton.layer.masksToBounds=YES;
self.headImageButton.layer.cornerRadius=self.headImageButton.frame.size.width/2;
NSData *headImageData;
headImageData=[NSKeyedArchiver archivedDataWithRootObject:self.headImageButton.imageView.image];
[[NSUserDefaults standardUserDefaults]setObject:headImageData forKey:@"headImage"];
}
}
#pragma mark
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITextField *myTextView1=[[UITextField alloc]init];
myTextView1=(UITextField *)[self.view viewWithTag:101];
if (![myTextView1 isExclusiveTouch]) {
[myTextView1 resignFirstResponder];
}
}
- (BOOL) imageHasAlpha: (UIImage *) image
{
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
return (alpha == kCGImageAlphaFirst ||
alpha == kCGImageAlphaLast ||
alpha == kCGImageAlphaPremultipliedFirst ||
alpha == kCGImageAlphaPremultipliedLast);
}
标签:
原文地址:http://www.cnblogs.com/liuyang666/p/5288358.html