首页 > 代码库 > iOS访问手机自带的相册

iOS访问手机自带的相册

1:首先遵守协议

UIImagePickerControllerDelegate,UINavigationControllerDelegate

2:

- (void)imageChange:(UIButton *)button{

    NSLog(@"调用系统相册");

   if (button!=self.button) {

       self.isChange=NO;

       self.button=button;

    }

    self.isChange=YES;

    UIImagePickerController *imagePickerController = [[UIImagePickerControlleralloc] init];

    imagePickerController.delegate =self;

    imagePickerController.allowsEditing =YES;

    imagePickerController.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

    [selfpresentViewController:imagePickerController animated:YEScompletion:^{}];

}

#pragma mark - 保存图片至沙盒

- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName

{

   NSData *imageData = http://www.mamicode.com/UIImageJPEGRepresentation(currentImage, 0.5);

    NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];

    [imageDatawriteToFile:fullPath atomically:NO];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    [picker dismissViewControllerAnimated:YEScompletion:^{}];

    UIImage *image = [infoobjectForKey:UIImagePickerControllerOriginalImage];

    [selfsaveImage:imagewithName:@"currentImage.png"];

    NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"currentImage.png"];

   if (self.isChange==YES) {

       self.savedImage = [[UIImagealloc] initWithContentsOfFile:fullPath];

        self.addImageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,88,87)];

        self.addImageView.layer.masksToBounds=YES;

        self.addImageView.layer.cornerRadius=44;

        self.addImageView.image=self.savedImage;

        [self.buttonaddSubview:self.addImageView];

    }elseif (self.isChange==NO){

       return ;

    }

    

}


iOS访问手机自带的相册