2013-02-20 52 views
1

你好,我有一个应用程序,拍照但不保存。我知道如何将图像保存到相册中,但我需要图像才能进入应用程序目录,并且不会显示在照片库中。我已经搜查过了,我没有找到一个工作。当我打开我的应用时,我也需要该图像在我的UIImageView中。这里是我的代码:将图像保存到应用程序目录中,而不是转到相册。 Objective C iOS

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> { 
    UIImagePickerController *picker; 
    UIImagePickerController *picker2; 
    UIImage *image; 
    IBOutlet UIImageView *imageView; 
} 

-(IBAction)TakePhoto; 
-(IBAction)ChooseExisting; 
- (IBAction)btnSave:(id)sender; 
- (IBAction)btnLoad:(id)sender; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

-(IBAction)TakePhoto{ 
    picker = [[UIImagePickerController alloc]init]; 
    picker.delegate=self; 
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    [self presentViewController:picker animated:YES completion:NULL]; 
    // [picker release]; 


} 

- (UIImage*)loadImage 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
         [NSString stringWithFormat: @"MyImage.png"] ]; 
    image = [UIImage imageWithContentsOfFile:path]; 
    return image; 
} 
-(IBAction)ChooseExisting{ 
    picker2 = [[UIImagePickerController alloc]init]; 
    picker2.delegate=self; 
    [picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    [self presentViewController:picker2 animated:YES completion:NULL]; 
    // [picker release]; 

} 

- (IBAction)btnSave:(id)sender { 

    if (image != nil) 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString* path = [documentsDirectory stringByAppendingPathComponent: 
          [NSString stringWithFormat: @"MyImage.png"]]; 
     NSData* data = UIImagePNGRepresentation(image); 
     [data writeToFile:path atomically:YES]; 
     NSLog(@"saved"); 
    } 
} 

- (IBAction)btnLoad:(id)sender { 
    [self loadImage]; 
} 



-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
    image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    [imageView setImage:image]; 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

- (void)viewDidLoad 
{ 
    [self loadImage]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

保存和的LoadImage方法不工作:(不知道为什么,但我没有任何错误

+0

检查您的图像是否存储在文档目录中。 – 2013-02-20 06:32:25

+0

你是否检查过,如果你触摸按钮,会调用'btnSave'?你在调试器中检查过'image','path','data'的值吗?你检查过'writeToFile:atomically:'的返回值吗? – 2013-02-20 06:52:14

+0

btnSave被调用,以检查图像是否稍后存储在文档目录中。还没有iPhone可以使用它。 – 2013-02-21 01:14:18

回答

2

您的图片保存/加载代码似乎是正确的。

检查您的IBActions是否在IB中正确连接。

而变化:

- (IBAction)btnLoad:(id)sender 
{ 
    [self loadImage]; 
} 

到:

- (IBAction)btnLoad:(id)sender 
{ 
    [self loadImage]; 
    [imageView setImage:image]; 
} 

你原来的代码似乎加载图像,但它并没有在任何地方表现出来。

编辑:

您可以随时检查图像是否保存到文档目录。 连接的设备在XCode中打开管理器窗口。

选择您的设备,然后选择应用程序。选择您感兴趣的应用程序 ,您应该看到与该 应用程序相关的文件树。您的图像文件是否列在“文档”文件夹中?

+1

,解决了我的问题最简单的方法:) ..谢谢你!也泰也所有其他的答案也:) – 2013-02-21 01:38:17

1

检查文档目录中的图像。你的电话[self loadImage];但loadImage返回你不在任何地方使用的UIImage。您可以将数据附加到选择器,如下所示。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyImage.png"]; 
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease]; 
[picker2 addAttachmentData:myData mimeType:@"image/png" fileName:@"MyImage"]; 
相关问题