【发布时间】:2014-12-23 12:31:38
【问题描述】:
我不太确定这里出了什么问题,我真的很感激一些帮助,因为我非常卡住。
这是我的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";
}
}
为什么我的图片没有加载? 非常感谢
【问题讨论】:
-
从 Documents 目录加载图片时,您确认文件路径正确吗?
-
为什么你给
[self dismissViewControllerAnimated:YES completion:NULL];打了两次电话? -
@Manesh 请使用这个 [self dismissViewControllerAnimated:YES completion:NULL];在最后的方法中行一次。让它使用这个编辑过的代码并告诉我。
-
@AdeelUrRehman 我已经删除了重复的dismissViewController,只保留了最后一个。程序仍然无法加载图像。
-
@SandeepAhuja 尝试过但没有任何作用
标签: ios xcode uiimagepickercontroller nsdata