gpt4 book ai didi

ios - 从 imagePickerController 将图像保存到文件 - 无法加载图像

转载 作者:行者123 更新时间:2023-11-29 02:25:21 25 4
gpt4 key购买 nike

我不太确定这里出了什么问题,我非常感谢您的帮助,因为我很困惑。这是我的 imagePickerController

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithLong:customphoto1] forKey:@"customphoto1count"];
UIImage *button1Image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];


pngData = UIImagePNGRepresentation(button1Image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Button1image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
[self dismissViewControllerAnimated:YES completion:NULL];
pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *imageLoaded = [UIImage imageWithData:pngData];
[_button1 setImage:imageLoaded forState:UIControlStateNormal];
self.button1.clipsToBounds = YES;
self.button1.layer.cornerRadius = (self.button1.frame.size.width / 16);//half of the width
self.button1.layer.borderColor=[UIColor blackColor].CGColor;
self.button1.layer.borderWidth=1.0f;
[self dismissViewControllerAnimated:YES completion:NULL];
}

上面保存了图像(据我所知),因为 _button1 正在从 imageLoaded 读取

当应用程序关闭并重新打开时,这就是调用要显示的按钮的内容,应该从我的 NSData 加载图片:

-(void)customButton1{
if (customphoto1 == 1) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Button1image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES];
pngData = [NSData dataWithContentsOfFile:filePath];
UIImage *imageLoaded1 = [UIImage imageWithData:pngData];
NSLog(@"%@", filePath);
// UIImage *imageload1 = [UIImage imageWithData:[newManagedObject valueForKey:@"imagesone"]];
[_button1 setImage:imageLoaded1 forState:UIControlStateNormal];

}
else {
button1ImageName = @"addnew";
}
}

为什么我的图片没有加载?非常感谢

最佳答案

试试这个:

-(void)customButton1{
if (customphoto1 == 1) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Button1image.png"];
UIImage *imageLoaded1 = [UIImage imagewithcontentsoffile:filePath];
NSLog(@"%@", filePath);

[_button1 setImage:imageLoaded1 forState:UIControlStateNormal];

}
else {
button1ImageName = @"addnew";
}
}

调试:检查打印的文件路径是否正确。在 Finder > CMD + Shift + G 中打开此路径并粘贴该路径(从路径中删除文件名,直到文件夹级别为止)。然后检查该文件夹中是否存在该特定图像。

同样在您的 didFinishPickingMediaWithInfo 方法中,您调用了 dismissViewControllerAnimated 方法两次,它应该是一次并且在方法的末尾。

希望对你有帮助

关于ios - 从 imagePickerController 将图像保存到文件 - 无法加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27620432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com