0

我正在使用 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];

}

非常感谢帮助

4

1 回答 1

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];
于 2011-04-06T10:13:17.990 回答