繁体   English   中英

将图像保存在App Directory中而不是转到相册。 Objective C iOS

[英]Saving an Image in App Directory instead of going to photo album. Objective C iOS

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

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

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

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

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

并改变:

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

至:

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

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

编辑:

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

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

检入文档目录中的图像。 你的召唤[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"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM