iPhone 调用本地相册 拍照

一、需要导入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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值