1

こんにちは、写真を撮っても保存しないアプリがあります。画像をフォト アルバムに保存する方法は知っていますが、画像をアプリのディレクトリに移動する必要があり、写真ギャラリーに表示されません。私はすでに検索しましたが、機能するものを探すことができませんでした。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 メソッドが機能しません:(。理由はわかりませんが、エラーはありません。

4

2 に答える 2

2

画像の保存/読み込みコードは正しいようです。

IBActionsIB で正しく接続されているかどうかを確認します。

そして変更:

- (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 に答える