相册相机访问

将一张图片写入iOS的相册中

- (void)saveImage
{
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //其中@selector的方法名不能瞎写,只能使用固定格式的
    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    //只能实现这个方法给@selector
    NSLog(@"Save success!");
}

通过相册或相机取得一张图片

如果要实现选取一张图片后拿到图片数据,要实现UINavigationControllerDelegate,UIImagePickerControllerDelegate代理的协议

- (IBAction)loadImage:(UIBarButtonItem*)item
{
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    pickerController.delegate = self;

    /*
   sourceType 的取值:
    UIImagePickerControllerSourceTypePhotoLibrary,
    UIImagePickerControllerSourceTypeCamera,
    UIImagePickerControllerSourceTypeSavedPhotosAlbum
    */
    pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    [self presentViewController:pickerController animated:YES completion:^{

    }];
}

//实现代理方法,取到图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
     UIImage *image = [[UIImageView alloc] initWithImage:(UIImage*)info[@"UIImagePickerControllerOriginalImage"]];
}

results matching ""

    No results matching ""