2

ここで指定されたコードを使用して、画像を保存およびロードしました

あるView Controllerで一緒に使用すると正常に動作しますが、あるView ControllerでsaveImageメソッドを使用し、別のView Controllerで画像を読み込もうとすると、画像が空白になりました...

ビューコントローラーAでは、次を使用して画像を保存します

- (void)saveImage: (UIImage*)image
{
    NSLog(@"saveimage called");

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

そして、View ControllerでBと言って、..を使用して画像をロードしています。

- (UIImage*)loadImage
{
    NSError *error;

    NSFileManager *fileMgr = [NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      @"test.png" ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];

    // Write out the contents of home directory to console
    NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

    return image;
}

コンソールでファイルの内容も取得しますが、画像が空白の理由がわかりません

2013-07-06 14:13:19.750 YouBank[500:c07] Documents directory: (
    "test.png"
)

私が間違っていることは何ですか...

EDIT:

viewDidloadメソッドのView Controller Bで、次のことを行います

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    ImageView.image=[self loadImage];
    ImageName.text = @"Cash Withdrawal";
}
4

2 に答える 2

1

これを試して

    //Document Directory
    #define kAppDirectoryPath   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)


    #pragma mark - File Functions - Document/Cache Directory Functions
    -(void)createDocumentDirectory:(NSString*)pStrDirectoryName
    {
        NSString *dataPath = [self getDocumentDirectoryPath:pStrDirectoryName];

        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:NULL];
    }

    -(NSString*)getDocumentDirectoryPath:(NSString*)pStrPathName
    {
        NSString *strPath = @"";
        if(pStrPathName)
            strPath = [[kAppDirectoryPath objectAtIndex:0] stringByAppendingPathComponent:pStrPathName];

        return strPath;
    }

写真を書くとき

        [self createDocumentDirectory:@"MyPhotos"];
        NSString  *pngPath = [NSHomeDirectory()  stringByAppendingPathComponent:strImageName];
        [UIImagePNGRepresentation(imgBg.image) writeToFile:pngPath atomically:YES];

ファイルを取得したら
編集

    NSError *error = nil;
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[FunctionManager getDocumentDirectoryPath:@"MyPhotos"] error:&error];
    if (!error) {
           NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
       NSArray *imagesOnly = [dirContents filteredArrayUsingPredicate:predicate];
            for (int i=0;i<[imagesOnly count]; i++) {
                [arrSaveImage addObject:[imagesOnly objectAtIndex:i]]; //arrSaveImage is Array of image that fetch one by one image and stored in the array
            }
 }


    NSString *strPath=[self getDocumentDirectoryPath:@"MyPhotos"]; // "MyPhotos" is Your Directory name
    strPath=[NSString stringWithFormat:@"%@/%@",strPath,[arrSaveImage objectAtIndex:i]]; //You can set your image name instead of [arrSaveImage objectAtIndex:i]
    imgBackScroll.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:strPath]];

それはあなたを助けるかもしれません。

于 2013-07-06T09:20:02.827 に答える