2011-04-06 47 views
0

我使用imagePickerController替換我的視圖中的現有圖像。該功能在同一個IB文件內工作。不過,我想將所選圖像加載到另一個IB-File中。我認爲解決方案是在保存時爲圖片命名。保存後,我想在我的另一個IB文件中從我的記憶中調用圖像(通過名稱)。如何從imagePickerController中爲保存的圖像命名

下面是一個代碼SNIPPIT我使用的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]; 

}

幫助是極大的讚賞

回答

1

的圖像拍攝方法,並把它保存在d irectory:

- (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]; 
} 

Lateron您可以通過使用稱之爲:

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

偉大的答案,也是解決我所期待的。非常感謝 – BarryK88 2011-04-06 11:32:41