【问题标题】:How to give a name to a saved Image from the imagePickerController如何为 imagePickerController 中保存的图像命名
【发布时间】:2011-07-30 16:37:12
【问题描述】:

我正在使用 imagePickerController 替换视图中的现有图像。该函数在同一个 IB 文件中工作。但是,我也想将所选图像加载到另一个 IB 文件中。我认为解决方案是在保存时为图像命名。保存后,我想在我的另一个 IB 文件中从我的内存中调用图像(按名称)。

这是我在 photopicker IB 文件中使用的代码片段

-(IBAction)setPhoto{

    image1.image = fotoView.image;
}

-(IBAction)getCameraPicture:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsImageEditing = YES;
    picker.sourceType = (sender == takePictureButton) ? UIImagePickerControllerSourceTypeCamera :
    UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentModalViewController: picker animated:YES];
    [picker release];
}

-(IBAction)selectExitingPicture
{
    if([UIImagePickerController isSourceTypeAvailable:
        UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *picker= [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingImage : (UIImage *)image
                 editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissModalViewControllerAnimated:YES];

    fotoView.image = image;

    NSData* imdata =  UIImagePNGRepresentation ( image );
    UIImage* im8 = [UIImage imageWithData:imdata];
    UIImageWriteToSavedPhotosAlbum(im8, nil, nil, nil);
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

在我的其他课程中,我想通过以下方式调用此图像:

if (#some condition){
        UIImage *img = [UIImage imageNamed:@"Name of the image.png"];
        [image1 setImage:img];

}

非常感谢您的帮助

【问题讨论】:

    标签: iphone save uiimagepickercontroller imagenamed


    【解决方案1】:

    图片拾取并保存到目录的方法:

    - (void)imagePickerController:(UIImagePickerController *)picker 
            didFinishPickingImage:(UIImage *)image
                      editingInfo:(NSDictionary *)editingInfo {
        [picker dismissModalViewControllerAnimated:YES];
        NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
        NSString *pngFilePath = [NSString stringWithFormat:@"%@/MyName.png",docDir];
        NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
        [data writeToFile:pngFilePath atomically:YES];
    }
    

    你可以通过以下方式调用它:

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/MyName.png",docDir];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:pngFilePath];
    

    【讨论】:

    • 很好的答案,也是我正在寻找的解决方案。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多