0

我无法在 iOS 中保存和检索图像。我通过这段代码得到 nil 图像:

-(NSString *)saveImageDataWithImage:(UIImage *)image
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *photoName = [NSString stringWithFormat:@"%ld.png",
                           (long)[[NSDate date] timeIntervalSince1970]];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:photoName];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:getImagePath])
    {
        NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
        [data1 writeToFile:getImagePath atomically:YES];
    }
    return photoName;
}
-(UIImage *)GetImageFromPath:(NSString *)ImageName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:ImageName];
    UIImage *thumbNail = [[UIImage alloc] initWithContentsOfFile:getImagePath];
    return thumbNail;
}

为什么我得到零图像?

请帮我。

4

2 回答 2

2
-(NSString *)saveImageDataWithImage:(UIImage *)image
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *photoName = [NSString stringWithFormat:@"%ld.png",
                           (long)[[NSDate date] timeIntervalSince1970]];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:photoName];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:getImagePath])
    {
        NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
        [data1 writeToFile:getImagePath atomically:YES];
    }
    return photoName;
}
-(UIImage *)GetImageFromPath:(NSString *)ImageName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:ImageName];
    UIImage *thumbNail = [[UIImage alloc] initWithContentsOfFile:getImagePath];
    return thumbNail;
}
于 2015-10-19T03:19:54.147 回答
0

看起来像您的目录名称中的拼写错误。请替换NSDocumentationDirectoryNSDocumentationDirectory它应该可以工作。

于 2015-09-18T12:24:39.100 回答