1

您好,我有一个可以拍照但不保存的应用程序。我知道如何将图像保存到相册,但我需要将图像转到应用程序的目录并且不会显示在照片库中。我已经搜索过了,但我没有找到一个可以工作的。UIImageView当我打开我的应用程序时,我还需要该图像。这是我的代码:

视图控制器.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

视图控制器.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

Save 和 loadImage 方法不起作用:(。不知道为什么,但我没有任何错误。

4

2 回答 2

2

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

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

并改变:

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

到:

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

您的原始代码似乎加载了图像,但它没有在任何地方显示。

编辑:

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

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

于 2013-02-20T06:52:15.503 回答
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"];
于 2013-02-20T06:58:01.037 回答