从plist保存并获取图像

编程入门 行业动态 更新时间:2024-10-25 02:26:16
本文介绍了从plist保存并获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个应用程序,用于从数组中获取图像并在ScrollView中垂直显示.

I am developing one app in that getting images from array and display vertically in ScrollView.

当用户双击特定图像时,我希望根据该图像的标签值将该确切图像存储到plist中,并在以后需要时检索该图像.

when user double tapped on particular image i want that exact image store into plist according to tag value of that image, and retrieve that image later on when require.

我尝试了这个

// 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数组中获取数据,请将其存储到滚动视图.

// For getting data from NSmutable array store it to the 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];

提前帮助我.

推荐答案

在.m文件顶部定义此MACRO定义

Define this MACRO Definition at the top of your .m file

#define LIB_DIR_PATH NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]

使用此功能可将图像保存到带有图像和名称的列表中

Use this function to Save Image to Plist with Image and Name

- (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返回相应名称的图像

This function returns image for respective name from 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"]; } }

更多推荐

从plist保存并获取图像

本文发布于:2023-07-16 07:27:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   plist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!