Ios 从plist保存并获取图像
我正在开发一个应用程序,从数组中获取图像并在ScrollView中垂直显示 当用户双击特定图像时,我希望根据该图像的标记值将该确切图像存储到plist中,并在以后需要时检索该图像 我试过这个Ios 从plist保存并获取图像,ios,objective-c,plist,Ios,Objective C,Plist,我正在开发一个应用程序,从数组中获取图像并在ScrollView中垂直显示 当用户双击特定图像时,我希望根据该图像的标记值将该确切图像存储到plist中,并在以后需要时检索该图像 我试过这个 // Store Data into plist. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
// Store Data into plist.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [NSString stringWithFormat:@"%@/myPlist.plist",
[paths objectAtIndex:0]];
// Place an image in a dictionary that will be stored as a plist
NSMutableDictionary * dictionary=[[NSMutableDictionary alloc]init];
[dictionary setObject:ImgView.tag forKey:@"image"];
NSLog(@"%@",dictionary);
// Write the dictionary to the filesystem as a plist
[NSKeyedArchiver archiveRootObject:dictionary toFile:path];
//要从NSmutable数组获取数据,请将其存储到scrollview
int m=0;
AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
delegate.front=TRUE;
delegate.back=FALSE;
UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[scrollView setPagingEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:NO];
FrontsCards=[[NSMutableArray alloc]initWithObjects:@"cloub1.png",@"cloub2.png",@"cloub3.png",@"cloub4.png",@"cloub5.png",@"cloub6.png",@"cloub7.png",@"cloub8.png",@"cloub9.png",@"cloub10.png",@"cloub11.png",@"cloub12.png",@"diamond1.png",@"diamond2.png",@"diamond3.png",@"diamond4.png",@"diamond5.png", nil];
for(m=0; m<[FrontsCards count];m++)
{
ImgView.alpha=1;
ImgView.tag=m;
int randIdx=arc4random()%[FrontsCards count];
NSString *imageName=[FrontsCards objectAtIndex:randIdx];
NSString *fullImageName=[NSString stringWithFormat:@"%@",imageName];
int padding=0;
CGRect imageViewFrame=CGRectMake(scrollView.frame.size.width*m+padding, scrollView.frame.origin.y, scrollView.frame.size.width-2*padding, scrollView.frame.size.height);
ImgView=[[UIImageView alloc]initWithFrame:imageViewFrame];
[ImgView setImage:[UIImage imageNamed:fullImageName]];
NSLog(@"%d",m);
// Place an image in a dictionary that will be stored as a plist
//[dictionary setObject:image forKey:@"image"];
// Write the dictionary to the filesystem as a plist
//[NSKeyedArchiver archiveRootObject:dictionary toFile:path];
[scrollView addSubview:ImgView];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapImgView:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delegate = self;
[self.ImgView addGestureRecognizer:doubleTap];
self.ImgView.userInteractionEnabled=YES;
}
CGSize scrollViewSize=CGSizeMake(scrollView.frame.size.width*[FrontsCards count], scrollView.frame.size.height);
[scrollView setContentSize:scrollViewSize];
[self.view addSubview:scrollView];
intm=0;
AppDelegate*委托=(AppDelegate*)[[UIApplication sharedApplication]委托];
delegate.front=TRUE;
delegate.back=FALSE;
UIScrollView*scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0320480)];
[滚动视图设置分页启用:是];
[滚动视图设置ShowShorizontalsCrollinIndicator:否];
FrontsCards=[[NSMutableArray alloc]initWithObjects:@“cloub1.png”,“cloub2.png”,“cloub3.png”,“cloub4.png”,“cloub5.png”,“cloub6.png”,“cloub7.png”,“cloub8.png”,“cloub9.png”,“cloub10.png”,“cloub11.png”,“cloub12.png”,“diamond1.png”,“diamond2.png”,“diamond3.png”,“diamond4.png”,diamond5.png];
对于(m=0;m,请在.m文件顶部定义此宏定义
#define LIB_DIR_PATH NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]
使用此功能将图像保存到Plist,并显示图像和名称
- (void)saveImage:(UIImage *)image WithName:(NSString *)imageName
{
// If File Exist then read it otherwise creat new
NSMutableDictionary *imageInfoDict;
if([[NSFileManager defaultManager] fileExistsAtPath:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]])
{
NSData *fileData = [NSData dataWithContentsOfFile:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]];
imageInfoDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:fileData]];
}
else
imageInfoDict = [NSMutableDictionary dictionaryWithCapacity:0];
// Add Single Image to Dictionary
[imageInfoDict setValue:image forKey:imageName];
// Convert Main info Dictionary to `NSData` to Save on Disc
[NSKeyedArchiver archiveRootObject:imageInfoDict toFile:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]];
// To Read Stored Image Use Following Code
[self readImageFromPlistByKey:imageName];
}
此函数用于从Plist返回各个名称的图像
-(UIImage *)readImageFromPlistByKey:(NSString *)keyName
{
// If File Exist then read it otherwise creat new
NSMutableDictionary *imageInfoDict;
if([[NSFileManager defaultManager] fileExistsAtPath:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]])
{
NSData *fileData = [NSData dataWithContentsOfFile:[LIB_DIR_PATH stringByAppendingPathComponent:@"imageInfo.plist"]];
if([fileData length] > 0)
{
// Read Plist
imageInfoDict = [NSMutableDictionary dictionaryWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithData:fileData]];
// Here is your Image
return imageInfoDict[keyName];
}
}
else
{
// Return Default Image if not Found
return [UIImage imageNamed:@"Default.png"];
}
}
您上面发布的代码不可能是真正的代码,因为它不会编译。也就是说,它显示了一些错误:
您不能将基本数字(NSInteger
)放入字典,它需要放入NSNumber
中
在创建图像视图的实例之前,您正在设置图像的标记(因此它将不执行任何操作或设置错误的标记)
要保存图像,如果确实要保存图像而不是标记,则需要将其另存为数据。可以将图像存储在词典中,这没有问题,但如果随后要将词典存储为plist,则需要将图像转换为NSData
。可以使用以下方法获取图像数据:
UIImageJPEGRepresentation(imageToSave, 0.8)
如果只存储索引,则需要一个主imageArray。我在用户双击imageView两次时添加了insert/delete
- (void)doubleTapImgView:(UITapGestureRecognizer *)recognizer
{
UIImageView *imageView = (UIImageView *)recognizer.view;
[self insertorDeleteImageIndex:imageView.tag-1];
}
- (NSString *)plistFilePath{
NSString *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
return [documents stringByAppendingPathComponent:@"ImageIndexes.plist"];
}
- (void)insertorDeleteImageIndex:(NSInteger)index{
NSString *filePath = [self plistFilePath];
NSMutableArray *savedIndexes = [NSMutableArray arrayWithContentsOfFile:filePath];
if (!savedIndexes) {
savedIndexes = [NSMutableArray array];
}
if (![savedIndexes containsObject:@(index)]) {
[savedIndexes addObject:@(index)];
}else{
[savedIndexes removeObject:@(index)];
}
[savedIndexes writeToFile:filePath atomically:YES];
}
- (NSArray *)savedImageIndexes{
NSString *filePath = [self plistFilePath];
return [NSArray arrayWithContentsOfFile:filePath];
}
你的问题是什么?我想将双分接图像保存到plist中。看,我的要求不同。我只想将双分接图像存储到plist中。这里的双分接图像是什么?将文件命名为.plist有点误导,因为它实际上是一个二进制文件,而不是plist…@Wain你可以相应地命名它。它的名称是just演示代码段不完整。当double时如何调用该方法tapped@JitendraDeore我已经包含了我为您编写的源代码。看看您哪里出了问题。@jitendradore我想我忘了在我的代码中包含它。请添加您的资源并从我的代码中删除硬编码的图像。它应该可以工作。