我不确定我在哪里做错了,这就是为什么我想在这里粘贴一些代码块以便找出问题所在。
我有一些具有透明背景的PNG图像,并且使用下面的代码块,我试图将所选图像保存到应用程序文件夹和NSUserDefault中。然后,我试图从应用程序文件夹中调用该保存的图像,并显示在UIBarButtonItem上。
保存到NSUserDefault的目的也是为了进行测试。
所以,
这是我选择图像的方式。
- (IBAction)btnPressed:(UIButton *)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
imgPicker.allowsEditing = NO;
[self presentViewController:imgPicker animated:YES completion:nil];
}
}
并且我使用didFinishPickingMediaWithInfo将其保存到应用程序中;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *) kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//I believe this has the problem.
NSData *pngData = UIImagePNGRepresentation(image);
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"AppImages"];;
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *fileName = [stringPath stringByAppendingPathComponent:@"logoImage.png"];
[pngData writeToFile:fileName atomically:YES];
NSUserDefaults *logoUserDef = [NSUserDefaults standardUserDefaults];
[logoUserDef setValue:pngData forKey:@"logoImageData"];
}
}
注意:对于没有透明背景的图像,我没有问题。
最后这就是我从Application和NSUserDefault调用它的方式。
同时使用NSUserDefaults和AppDirectory查看它们是否显示不同的结果。
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"logoImageData"])
{
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
UIImage *testing = [UIImage imageWithData:[user valueForKey:@"logoImageData"]];
NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
UIImage * image = [self loadImageWithFileName:@"logoImage" ofType:@"png" inDirectory:documentsDirectory];
UIBarButtonItem *logoBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(logoTapped)];
logoBarButton.imageInsets = UIEdgeInsetsMake(9, 0, 9, 90);
logoBarButton.image = [logoBarButton.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.navigationItem setLeftBarButtonItems:@[logoBarButton]];
}
和从App Document返回UIImage的方法。
-(UIImage *)loadImageWithFileName:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/%@.%@", directoryPath, @"AppImages", fileName, [extension lowercaseString]]];
return result;
}
我不知道我在哪里做错了,但与往常一样,当我选择具有透明背景的图像时。总是将其背景转换为白色。我需要保存它,因为它是透明的。我进行了很多搜索,但找不到任何解决方法。
感谢您的任何帮助。
解决了问题!
我在问题中发布的代码块没有错。问题是从iTunes发生的,当我将Mac上的图像同步到iPhone时,iTunes将它们转换为JPEG,并且由于该图像失去了透明度等...,当我使用我的应用选择图像时,我选择的图像是JPEG,而没有被意识到,这就是为什么我遇到这些问题。
所以我想出了一种通过电子邮件获取图像的解决方案。我只是通过电子邮件将它们从我的Mac发送到iPhone,并且此图像作为一项功能没有丢失。之后,当我在应用程序中将它们用作正确的图像时,该程序将按预期的方式工作。
您写的
[logoUserDef setValue:pngData forKey:@"logoImageData"];
但我看不到您在哪里同步。因此添加:
[logoUserDef synchronize];
使用后将图像保存到Documents目录时,我能够保持透明度
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage];
[yourButton setImage:img forState:UIControlStateNormal];
NSData *pngData = UIImagePNGRepresentation(img);
//do something with pngData
}
键是UIImagePickerControllerOriginalImage,而不是其他选项,例如UIImagePickerControllerEditedImage。