图像字符串的数据为零。

时间:2016-03-31 20:14:24

标签: ios objective-c uiimage nsdate

这是我的保存方法:

-(NSString *)saveImage:(UIImage *)image
{
    NSInteger RandomIndex = arc4random() % 1000;
    NSString *randomImageName =[NSString stringWithFormat:@"Image%i.png",RandomIndex];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:randomImageName];
    if ([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:savedImagePath error:nil];
        NSLog(@"file removed from path");
    }
    NSLog(@"Saved Image Path : %@",savedImagePath);
    NSData* imageData = UIImagePNGRepresentation (image);
    [imageData writeToFile:savedImagePath atomically:YES];

    self.teamLogo = savedImagePath;

    return savedImagePath;
}

我尝试加载图片并加以控制:

NSData *myData = [NSData dataWithContentsOfFile:self.team.logo];
    UIImage *selectedImage = [UIImage imageWithData:myData];

self.team.logo来自DB。我把弦保持在基地。在调试模式下,我得到了这个字符串:

/var/mobile/Containers/Data/Application/7CD69EC9-9C20-48EE-B611-FC2353BC31B0/Documents/Image364.png

和myData仍为零。你知道为什么吗?

1 个答案:

答案 0 :(得分:0)

从IOS 8开始,容器布局发生了变化。因此,请不要存储数据库的完整路径。而只是存储您的图像名称。要调用图像,请使用:

$arrayas = json_decode($array);
foreach($arrayas $i){

        foreach($i as $z){

            foreach($z as $key => $value){

                print_r($key);

            }  
        }
    }
}

我希望它有所帮助。