一、需要导入UIImagePickerControllerDelegate, UINavigationControllerDelegate 代理
二 、
@interface FaceView()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIButton *imageButton;
}
@end
@implementation FaceView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[MySingletonClassesshareMySingleton].replyImagePath = nil;
imageButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(10, 10, 50, 50);
[imageButtonsetBackgroundImage:[UIImage imageNamed:@"chatpicture.png"] forState:UIControlStateNormal];
[imageButtonaddTarget:selfaction:@selector(addImageClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:imageButton];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(btnLong:)];
longPress.minimumPressDuration = 0.8; //定义按的时间
[imageButton addGestureRecognizer:longPress];
}
returnself;
}
- (void)addImageClick
{
deleButton.hidden = YES;
UIActionSheet *menu=[[UIActionSheetalloc] initWithTitle:@"上传图片" delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照上传",@"从相册上传", nil];
menu.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
[menu showInView:self];
}
#pragma mark UIActionSheet -Delegate
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
[MySingletonClassesshareMySingleton].imageSelectBool = YES;
if(buttonIndex==0){
// 拍照
UIImagePickerController *ipc=[[UIImagePickerControlleralloc] init];
ipc.sourceType=UIImagePickerControllerSourceTypeCamera;
ipc.delegate=self;
ipc.allowsEditing=NO;
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
[app.window.rootViewControllerpresentModalViewController:ipc animated:YES];
}else if(buttonIndex==1){
// 相册里找
UIImagePickerController *ipc=[[UIImagePickerControlleralloc] init];
ipc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate=self;
ipc.allowsEditing=NO;
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
[app.window.rootViewControllerpresentModalViewController:ipc animated:YES];
}
[actionSheet release];
}
#pragma mark 选择图片附件
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *) info{
if(picker.sourceType==UIImagePickerControllerSourceTypeCamera){
// UIImage* original_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// UIImageWriteToSavedPhotosAlbum(original_image, self,
// @selector(image:didFinishSavingWithError:contextInfo:),
// nil);
}
// UIImage *img=[info objectForKey:@"UIImagePickerControllerEditedImage"];
UIImage *img=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImage *newImg=[selfimageWithImageSimple:img scaledToSize:CGSizeMake(300, 300)];
[imageButtonsetBackgroundImage:newImg forState:UIControlStateNormal];
[selfsaveImage:newImg WithName:[NSStringstringWithFormat:@"%@%@",[selfgenerateUuidString],@".png"]];
AppDelegate *app = (AppDelegate *)[UIApplicationsharedApplication].delegate;
[app.window.rootViewControllerdismissModalViewControllerAnimated:YES];
[picker release];
}
-(UIImage *) imageWithImageSimple:(UIImage*) image scaledToSize:(CGSize) newSize{
newSize.height=image.size.height*(newSize.width/image.size.width);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
{
NSData* imageData = UIImagePNGRepresentation(tempImage);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
NSLog(@"--fullPathToFile:%@",fullPathToFile);
[imageData writeToFile:fullPathToFile atomically:NO];
}
//随机生产图片的名字
- (NSString *)generateUuidString
{
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
[uuidString autorelease];
CFRelease(uuid);
return uuidString;
}