主要是一个图片展示的项目,现在才发现,项目前期的规划很重要,本文记录下项目开发中遇到的问题,
1 动画的实现,在界面内到处飘荡的蒲公英,简单的使用了uiview的动画和时间函数NStimer实现的,代码大致如下:
puGongYingTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(runPuGongYing) userInfo:nil repeats:NO];//时间函数 bigPuGongYingGif =[[GifView alloc] initWithFrame:CGRectMake(-20, 50, 45,46) filePath:[[NSBundle mainBundle] pathForResource:@"pugongying" ofType:@"png"]]; //上面的是GIf图片的实现开始的动画实现
[bgView addSubview:bigPuGongYingGif]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDelegate:self]; [UIView setAnimationDuration:50]; int startX = round(random() % 900);//随机的路径 int endX = round(random() % 700); NSLog(@"startX===%d endX===%d",startX,endX); bigPuGongYingGif.frame=CGRectMake(1025, endX, 45, 46); [UIView setAnimationDidStopSelector:@selector(runPuGongYingAgain)];//循环调用 [UIView commitAnimations];
2 在appdelegate里面页面的跳转方法,先设置为空,然后设置为需要的VC
self.window.rootViewController=Nil;、、
PingBao_PageViewController *pic_page=[[PingBao_PageViewController alloc]init];
self.window.rootViewController=pic_page;
3 app不让自动锁屏幕 [UIApplication sharedApplication].idleTimerDisabled=YES;
4单例模式的使用非常有用,下面给一个例子,方面以后使用
#import <Foundation/Foundation.h> @interface DataPist : NSObject{ NSMutableDictionary *arryData; } @property(nonatomic,strong)NSMutableDictionary *arryData; @property(nonatomic,strong)NSArray *jianBaoArry; @property(nonatomic,strong)NSString *ipString; +(DataPist *) shared; +(id) allocWithZone:(NSZone *)zone; +(NSData*)stringToByte:(NSString*)string; +(void)showLoading; +(void)hideLoading; + (NSString *)dataFilePath ; @end #import "DataPist.h" @implementation DataPist @synthesize arryData,jianBaoArry,ipString; static DataPist *ShareDataPist = nil; +(DataPist *) shared{ @synchronized(self) { if (ShareDataPist == nil) { ShareDataPist = [[self alloc] init] ; } } return ShareDataPist; } +(id) allocWithZone:(NSZone *)zone { @synchronized(self) { if (ShareDataPist == nil) { ShareDataPist = [super allocWithZone:zone]; return ShareDataPist; } } return nil; } //转码函数16进止的图片转换的NSData +(NSData*)stringToByte:(NSString*)string { NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""]; if ([hexString length]%2!=0) { return nil; } Byte tempbyt[1]={0}; NSMutableData* bytes=[NSMutableData data]; for(int i=0;i<[hexString length];i++) { unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16) int int_ch1; if(hex_char1 >= '0' && hex_char1 <='9') int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48 else if(hex_char1 >= 'A' && hex_char1 <='F') int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65 else return nil; i++; unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位) int int_ch2; if(hex_char2 >= '0' && hex_char2 <='9') int_ch2 = (hex_char2-48); //// 0 的Ascll - 48 else if(hex_char2 >= 'A' && hex_char2 <='F') int_ch2 = hex_char2-55; //// A 的Ascll - 65 else return nil; tempbyt[0] = int_ch1+int_ch2; ///将转化后的数放入Byte数组里 [bytes appendBytes:tempbyt length:1]; } return bytes; } +(void)showLoading{ // [[UIApplication sharedApplication].windows objectAtIndex:0] [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication].windows objectAtIndex:0] animated:YES]; // mb.labelText=@"正在加载"; } +(void)hideLoading{ [MBProgressHUD hideHUDForView:[[UIApplication sharedApplication].windows objectAtIndex:0] animated:YES]; } + (NSString *)dataFilePath { //写入library NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Library"]; NSString *pathNext = [NSString stringWithFormat:@"%@/Caches",path]; NSString *fileNamepath=[pathNext stringByAppendingPathComponent:@"Image.plist"]; return fileNamepath; } @end
5 下载多个zip文件图片,然后解压,放入指定的目录,实现
下载实现:
//宏定义沙盒目录#define DocumentsDirectory [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject] -(void)download{ mb.labelText=@"正在下载"; // 初始化队列 if (!networkQueue) { networkQueue = [[ASINetworkQueue alloc] init]; // 设置最大并发数 默认为-1 无限制 [networkQueue setMaxConcurrentOperationCount:-1]; } // 重制队列 [networkQueue cancelAllOperations]; [networkQueue reset]; // 设置队列的进度条 [networkQueue setDownloadProgressDelegate:progressView]; // 设置完成方法 [networkQueue setRequestDidFailSelector:@selector(Failed:)];//一个文件下载失败执行 [networkQueue setQueueDidFinishSelector:@selector(Succeed:)];//不管下载成功失败都会执行的 [networkQueue setRequestDidFinishSelector:@selector(Finished:)];//一个成功就执行 // 显示精确进度 [networkQueue setShowAccurateProgress:YES]; [networkQueue setDelegate:self]; ASIHTTPRequest *request; //zip_names从服务器去的zipname地址 for (int i=0; i<[zip_names count]; i++) { NSString *name=[zip_names objectAtIndex:i]; NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/api/%@.zip",[DataPist shared].ipString,name]]; request = [ASIHTTPRequest requestWithURL:url];
[request setTemporaryFileDownloadPath:[DocumentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip.temp",[zip_names objectAtIndex:i]]]]; [request setDownloadDestinationPath:[DocumentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",[zip_names objectAtIndex:i]]]]; [request setAllowResumeForFileDownloads:YES]; [networkQueue addOperation:request]; } [networkQueue go]; } -(void)Succeed:(ASINetworkQueue *)request{ if (Flag != NO) { [self performSelector:@selector(changeloading) withObject:self afterDelay:0]; [self jieya]; } } -(void)Finished:(ASIHTTPRequest *)request{ } -(void)changeloading{ mb.labelText=@"正在初始化数据"; } -(void)Failed:(ASIHTTPRequest *)request{ Flag = NO;//一个文件下载失败的时候设置为no,将不会执行Succeed:(ASINetworkQueue *)request里面的解压方法 if (IsFail==NO) { [ MBProgressHUD hideHUDForView:self.view animated:YES]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络更新失败,请重新下载" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:Nil, nil]; [alert show]; IsFail=YES;变成yes后,他就只执行一次, } progressView.hidden=YES; [ MBProgressHUD hideHUDForView:self.view animated:YES]; }
//IsFail=NO下载失败,为了不让重复出现下载失败的弹出框;
//Flag = YES;作用是显示只有下载成功后才开始执行 Succeed:(ASINetworkQueue *)request里面的方法,这是他们的初始化值,
//当失败后,又的从新开始他们的数据初始化,IsFail=no,Flag=yes
//解压方法
- (void)jieya{
要用到zipArchive的类库
ZipArchive *zip=[[ZipArchive alloc]init];
for (int i = 0; i < zip_names.count; i++) {
NSString *name = [NSString stringWithFormat:@"Documents/%@.zip",[zip_names objectAtIndex:i]];
NSString *path=[NSHomeDirectory()stringByAppendingPathComponent:[NSString stringWithString:name]];
NSLog(@"-----%@",path);
NSString *unZipTo;
NSString *rang=[zip_names objectAtIndex:i];
// 这块是为了找到下载的zip是那种,然后分开设置放到不同的文件夹里,
if (([rang rangeOfString:@"large"].location!=NSNotFound)) {
unZipTo = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/bigImage"];
}else{
unZipTo = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
}
if ([zip UnzipOpenFile:path])
{
[zip UnzipFileTo:unZipTo overWrite:YES];
[zip UnzipCloseFile];
}
}
if ([[zip_names objectAtIndex:0] rangeOfString:@"small"].location!=NSNotFound||[[zip_names objectAtIndex:0] rangeOfString:@"publicimg"].location!=NSNotFound) {
[self writer];
}else{
NSLog(@"大图不需要写入");
}
写入的方法,当时想的是哦把图片存到plist文件了,
-(void)writer{
//拿到应用程序沙盒里面的路径
// NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
//读取存在沙盒里面的文件图片
for (int i = 0; i < zip_names.count; i++) {
if ([[zip_names objectAtIndex:i] isEqualToString:@"publicimg"] || [[zip_names objectAtIndex:i] isEqualToString:@"uppublicimg"]) {
NSString *imgPath1=[NSHomeDirectory()stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@.json",[zip_names objectAtIndex:i]]];
////因为拿到的是个路径 把它加载成一个data对象
NSData *data=[NSData dataWithContentsOfFile:imgPath1];
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:NULL];
//判断登录是否正确
// NSString *str = [NSString stringWithFormat:@"%@",[weatherDic objectForKey:@"flag"]];
//取出所有图片信息
NSDictionary *season = [weatherDic objectForKey:@"season_img"];
NSDictionary *season_line = [weatherDic objectForKey:@"line_season_img"];
// NSDictionary *report_img = [weatherDic objectForKey:@"report_img"];
NSDictionary *screen_img = [weatherDic objectForKey:@"screen_img"];
// NSLog(@"%@,%@,%@,%@,%@",wirte,season,season_line,report_img,screen_img);
// NSDictionary *line_name = [NSDictionary alloc];
//写入plist
// NSString *filePath = [self dataFilePath];
if ([DataPist shared].arryData ==Nil) {判断arryData是否存在,不存在的话,从新创建文件路径
[DataPist shared].arryData = [[NSMutableDictionary alloc]init];
}
if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]) {
[DataPist shared].arryData = [[NSMutableDictionary alloc]initWithContentsOfFile:[self dataFilePath]];
}
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:[self dataFilePath] contents:nil attributes:nil];
if (![[NSString stringWithFormat:@"%@",[season objectForKey:@"img_null"]] isEqualToString:@"0"]) {
[ [DataPist shared].arryData setValuesForKeysWithDictionary:season];//这中setvalue的dic是直接写入的plsit里面添加的,他会自动写入key value,
}
if (![[NSString stringWithFormat:@"%@",[season_line objectForKey:@"img_null"]] isEqualToString:@"0"]) {
[ [DataPist shared].arryData setValuesForKeysWithDictionary:season_line];
}
// if (![[NSString stringWithFormat:@"%@",[report_img objectForKey:@"img_null"]] isEqualToString:@"0"]) {
// [ [DataPist shared].arryData setValuesForKeysWithDictionary:report_img];
// }
if (![[NSString stringWithFormat:@"%@",[screen_img objectForKey:@"img_null"]] isEqualToString:@"0"]) {
[ [DataPist shared].arryData setValuesForKeysWithDictionary:screen_img];
}
}
else if([[zip_names objectAtIndex:i] rangeOfString:@"small"].location!=NSNotFound){
NSString *imgPath1=[NSHomeDirectory()stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@.json",[zip_names objectAtIndex:i]]];
////因为拿到的是个路径 把它加载成一个data对象
NSData *data=[NSData dataWithContentsOfFile:imgPath1];
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:NULL];
NSDictionary *img_name = [weatherDic objectForKey:@"line_img"];
if (![[NSString stringWithFormat:@"%@",[img_name objectForKey:@"img_null"]] isEqualToString:@"0"]) {
[ [DataPist shared].arryData setValuesForKeysWithDictionary:img_name];
}
}else{
}
[ [DataPist shared].arryData setValue:line_img forKey:@"line_name"];
//-------------------
[ [DataPist shared].arryData setValue:screen_id forKey:@"screen_id"];
[ [DataPist shared].arryData writeToFile:[self dataFilePath] atomically:YES];
}
isDown=@"1";
[self downLoadAgain];
[DataPist hideLoading];
Choose_LineViewController *choose_Line = [[Choose_LineViewController alloc]init];
[[NSUserDefaults standardUserDefaults] setObject:leftrightImageUrl forKey:@"leftOrRightUrl"];
choose_Line.urlDic=leftrightImageUrl;
[self presentViewController:choose_Line animated:YES completion:Nil];
}
//成功后的判断
-(void)downLoadAgain{
NSString *uuid = [[UIDevice currentDevice] uniqueIdentifier];
NSString *post = [NSString stringWithFormat:@"%@/api/flag.php?uid=%@&status=%@",[DataPist shared].ipString,uuid,isDown];
NSURLRequest *request111 = [NSURLRequest requestWithURL:[NSURL URLWithString:post]];
NSData *response = [NSURLConnection sendSynchronousRequest:request111 returningResponse:nil error:nil];
// update_flg=@"1";
// [[NSUserDefaults standardUserDefaults]setObject:update_flg forKey:@"update_flg"];
}
上面主要用到了常规的方法,还有强大的BOOl变量,可以判断许多东西方法是否执行,以后记得使用,
6:ipad上面的弹出视图:用下面这周方式,类似qq弹出设置界面的
Setting_PageViewController *setting_page = [[Setting_PageViewController alloc]init]; UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:setting_page]; nav.modalPresentationStyle=UIModalPresentationFormSheet; [self presentViewController:nav animated:YES completion:NULL];
如果弹出界面有多层的视图,可以使用这样push进去的,(用的是tableView的方法)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section==0) { switch (indexPath.row) { case 0: { ScreenSaveAnimation_ViewController *SV=[[ScreenSaveAnimation_ViewController alloc]init]; [self.navigationController pushViewController:SV animated:YES]; } break; case 1:{ ScreenSaveType_ViewController *ST=[[ScreenSaveType_ViewController alloc]init]; [self.navigationController pushViewController:ST animated:YES]; }break; default: break; }
push进去的视图,返回的时候也可以返回 [self.navigationController popViewControllerAnimated:YES];
在tableview中int n=[indexPath indexAtPosition:0];可以取到section的位置,主要用在tableview中的didSelectRowAtIndexPath方法里面,在点击此方式的时候,为了在列表的左边显示一个记号的话,可以在开始的时候在每个rows里都添加记号的按钮,先隐藏掉,设置tag,然后,在点击的时候通过tag让他显示出来,就是先把tableview上的view全部遍历一遍,都隐藏,然后找出你点击的view的tag,让他显示,别的也就都隐藏了,类似此类的代码
-(void)nextTag:(NSIndexPath *)indexPath{ UIImageView *image; int n=[indexPath indexAtPosition:0];//区分section if (n==0) { for (int i=500; i<540; i++) {//先全部循环隐藏掉, image=(UIImageView *)[self.view viewWithTag:i]; [image setHidden:YES]; } image=(UIImageView *)[self.view viewWithTag:indexPath.row+500];//某一个显示 image.hidden=NO; }else if (n==1){ for (int i=500; i<540; i++) { image=(UIImageView *)[self.view viewWithTag:i]; [image setHidden:YES]; } image=(UIImageView *)[self.view viewWithTag:indexPath.row+510]; image.hidden=NO; }else if (n==2){ for (int i=500; i<540; i++) { image=(UIImageView *)[self.view viewWithTag:i]; [image setHidden:YES]; } image=(UIImageView *)[self.view viewWithTag:indexPath.row+520]; image.hidden=NO; }else if (n==3){ for (int i=500; i<540; i++) { image=(UIImageView *)[self.view viewWithTag:i]; [image setHidden:YES]; } image=(UIImageView *)[self.view viewWithTag:indexPath.row+530]; image.hidden=NO; } }
7 用button显示九宫格的的方法,
-(void)initImage:(int)number { 此方法主要用于现在在scrollview上分页加载显示图片的, int num = line_detailInfo.count - number*10;//判断line_detailInfo是总得图片数, //num11 判断行数 int num11 = 0; //num22 判断列数 int num22 = 0; if (num > 5) { num11 = 2; }else if(num <= 5 ){ num11 = 1; } for (int M=0; M < num11; M++) { if (num >= 5) { num22 = 5; }else if(num < 5){ num22 = num%5; } for (int i=0; i < num22; i++) { UIButton *btns = [UIButton buttonWithType:0]; // [btns setImage:[UIImage imageNamed:[NSString stringWithFormat:@"a_%d.png",i]] forState:UIControlStateNormal]; NSString *str; if (M == 0) { str = [line_detailInfo objectAtIndex:i+number*10]; }else if (M == 1){ str = [line_detailInfo objectAtIndex:i+number*10+5]; } NSString *img_data = [NSString stringWithFormat:@"%@",[[DataPist shared].arryData objectForKey:[NSString stringWithFormat:@"s%@",str]]]; [btns setImage:[UIImage imageWithData:[DataPist stringToByte:img_data]] forState:UIControlStateNormal];//显示图片 if (M == 0) { btns.tag = i+number*10+100; }else if (M == 1){ btns.tag = i+number*10+5+100; } [btns addTarget:self action:@selector(goNext:) forControlEvents:UIControlEventTouchUpInside]; btns.frame = CGRectMake(1024*number+99+i*130+i*45,M*230+41*M+70, 130,230); [scroll addSubview:btns]; } num = num - 5; } }
下面的方法主要显示的在scrollview上面当加载到下一页的时候,当你放回的时候不用再加载
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ int n=scrollView.contentOffset.x/1024; if (n >= scor) { scor = n; [self initImage:n]; } }
用scrollview显示多张 图片,类似于图片浏览器
scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 82, 1024, 630)]; scroll.backgroundColor=[UIColor clearColor]; scroll.showsVerticalScrollIndicator=NO; scroll.pagingEnabled=YES; scroll.delegate=self; scroll.showsVerticalScrollIndicator = FALSE; scroll.showsHorizontalScrollIndicator = FALSE; [self.view addSubview:scroll];底层的scrollview用于滑动和分页用的
//这MRZoomScrollView主要用与显示图片和放大图片的
for (int i = 0; i < imageArrys.count; i++) { zoomScrollView = [[MRZoomScrollView alloc]init]; CGRect frame = scroll.frame; zoomScrollView.maximumZoomScale=2; zoomScrollView.minimumZoomScale=1; zoomScrollView.tag=2000+i; zoomScrollView.showsVerticalScrollIndicator = NO; zoomScrollView.showsHorizontalScrollIndicator=NO; // zoomScrollView.backgroundColor=[UIColor grayColor]; frame.origin.x = frame.size.width * i; frame.origin.y = 0; zoomScrollView.frame = frame; [scroll addSubview:zoomScrollView]; } scroll.contentSize=CGSizeMake(1024*imageArrys.count, 768);设置图片共有多少张 [scroll setContentOffset:CGPointMake(1024*currentNum, 0) animated:YES];//设置点击小图进入大图的时候scroll停留在那个位置。
MRZoomScrollView自定义的类显示图片的scrollview
@interface MRZoomScrollView : UIScrollView <UIScrollViewDelegate>
{
UIImageView *imageView;
}
@property (nonatomic, retain) UIImageView *imageView;
@end
#import "MRZoomScrollView.h"
#define MRScreenWidth CGRectGetWidth([UIScreen mainScreen].applicationFrame)
#define MRScreenHeight CGRectGetHeight([UIScreen mainScreen].applicationFrame)
@interface MRZoomScrollView (Utility)
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end
@implementation MRZoomScrollView
@synthesize imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
self.frame = CGRectMake(0, 0, MRScreenWidth, MRScreenHeight);
[self initImageView];
}
return self;
}
- (void)initImageView
{
imageView = [[UIImageView alloc]init];
// The imageView can be zoomed largest size
imageView.frame = CGRectMake(224, 0, 576, 768);
imageView.userInteractionEnabled = YES;
[self addSubview:imageView];
}
#pragma mark - Zoom methods
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
NSLog(@"------%@",NSStringFromCGSize(scrollView.contentSize));
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?
(scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?
(scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
// if (YES) {
imageView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
scrollView.contentSize.height * 0.5 + offsetY);
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
#pragma mark - View cycle
@end
主要用于显示党scrollview滑动超过一屏时才执行的方法,
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (scrollView.contentOffset.x>1024*n||scrollView.contentOffset.x<=1024*(n-1)) {//判断是否滑动一屏
n=scrollView.contentOffset.x/1024;
[self initImage:n];
}
}
苹果自带的简单的网络请求方法 并且解析数据
-(void)getInfoImage{ NSString *urlString = [NSString stringWithFormat:@"%@/api/model_style.php?img_name=%@",[DataPist shared].ipString,[imageArrys objectAtIndex:tags]]; NSURLResponse *urlResponce=nil; NSError *error=nil; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; NSData *Data=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponce error:&error]; if (error) { [DataPist hideLoading]; UIAlertView *alter=[[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络不可用,请稍后再试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alter show]; return; } NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:Data options:NSJSONReadingMutableLeaves error:nil];
还有当滑动的时候清除一些控件的值,为了防止重叠,让他从新显示,如果用viewwithtag比较麻烦的话,可以使用简单的把需要从新赋值的控件放到一个view上面,然后通过移出view,新建view从新赋值,
8:如果客户端需要查询图片分类的方法。一般的情况是需要数据库的支持,但是为了方便的话,可以点击按钮,像服务器短发请求,通过服务器查询后返回需要的图片的显示,然后客户端接受图片,然后在沙盒里面查询,这样还快,比较简单,显示沙盒里面的图片的简单的方法如下
NSString *name=[NSString stringWithFormat:@"%@",[imageArrys objectAtIndex:tags]];图片名 NSString *ss=[NSString stringWithFormat:@"Documents/bigImage/%@",name]; NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",ss]];图片路径 NSString *fileName=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];/加载图片,转为字符串 NSString *imageNames=[fileName stringByReplacingOccurrencesOfString:@"-" withString:@"1"];//解密加密的图片字符串
9 视频的加载用到的是AVQueuePlayer,简单的代码如下
//初始化播放器组件 -(void)initPlayer { [arryItems removeAllObjects]; for (int i = 0; i < 34; i++) { [arryMp3Name addObject:[NSString stringWithFormat:@"序列 %@",[arryImages objectAtIndex:i]]]; } for (NSString *text in arryMp3Name) { NSURL *playerFileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:text ofType:@"mp4"]]; AVPlayerItem *item=[AVPlayerItem playerItemWithURL:playerFileURL]; [arryItems addObject:item]; } // NSLog(@"arryItems==%d",arryItems.count); //添加播放下首歌的通知 for (int songPointer = 0; songPointer < [arryItems count]; songPointer++) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(songEnded:) name:AVPlayerItemDidPlayToEndTimeNotification object:[arryItems objectAtIndex:songPointer]]; } queuePlayer = [AVQueuePlayer queuePlayerWithItems:arryItems]; playLayer = [AVPlayerLayer playerLayerWithPlayer:queuePlayer]; playLayer.frame = CGRectMake(0, 82, 1024, 576); [self.view.layer addSublayer:playLayer]; [queuePlayer play]; playerView=[[UIView alloc]initWithFrame:CGRectMake(0,90, 1024, 576)]; playerView.backgroundColor=[UIColor clearColor]; playerView.userInteractionEnabled=YES; [self.view addSubview:playerView]; } -(void)songEnded:(NSNotification *)notification{ // NSLog(@"---%@",notification.object); // NSLog(@"--%d",n); // NSLog(@"-----------------------%d",littletag); if (Flag == YES) { n = littletag; Flag = NO; } //动画实现添加蒙板 [UIView beginAnimations:@"Curl"context:nil];//动画开始 [UIView setAnimationDuration:0.75]; [UIView setAnimationDelegate:self]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [(UIButton *)[self.view viewWithTag:n+1+1000] setAlpha:1];//主要是视频下面的饿缩略图的显示随着视频的播放显示 [(UIButton *)[self.view viewWithTag:n+1000] setAlpha:0.2]; [UIView commitAnimations]; n++; //当n=34,播放到最后一个的时候,重新初始化播放器 if (n==34) { n=0; [(UIButton *)[self.view viewWithTag:n+1000] setAlpha:1]; [self initPlayer]; [scoll setContentOffset:CGPointMake(0,scoll.contentOffset.y) animated:YES]; } if(n>=10&&n<27){ [scoll setContentOffset:CGPointMake(scoll.contentOffset.x+90,scoll.contentOffset.y) animated:YES]; } }
//返回到别的页面,要注销所有通知
//当返回上级时移除所有通知
- (void)releaseQueuePlayer
{
n=0;
for (int songPointer = 0; songPointer < [arryItems count]; songPointer++) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:[arryItems objectAtIndex:songPointer]];
}
[queuePlayer removeAllItems];
[arryItems removeAllObjects];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"startMovie" object:nil];
[queuePlayer pause];
}
、、点击下面某个缩略图,播放某个视频,然后接着下面的后面的继续播放
//返回到到播放页面的方法
-(void)replayMovie:(NSNotification *)tag
{
// NSLog(@"----%@",tag.object);
[queuePlayer removeAllItems];
// NSLog(@"***************%d",[tag.object intValue]);
littletag=[tag.object intValue];
for (int i = littletag; i <arryItems.count; i++) {
AVPlayerItem* obj = [arryItems objectAtIndex:i];
if ([queuePlayer canInsertItem:obj afterItem:nil]) {
[obj seekToTime:kCMTimeZero];
[queuePlayer insertItem:obj afterItem:nil];
}
}
for (int i=0; i<34; i++) {
[(UIButton *)[self.view viewWithTag:1000+i]setAlpha:0.2];
}
[(UIButton *)[self.view viewWithTag:littletag+1000]setAlpha:1];
[queuePlayer play];
// if(littletag>=10){
// [scoll setContentOffset:CGPointMake(scoll.contentOffset.x+90,scoll.contentOffset.y) animated:YES];
// }
}
上面就是项目中学到的东西,希望以后能注意,铭记