孙同生的博客

为美好的世界献上祝福

OS X 10.8或iOS 6以及之后版本中使用,Dispatch Queue将会由ARC自动管理,不需要手动释放

队列

分为串行队列和并发队列

阅读全文 »

由于UIImage的imageWithData函数是每次画图的时候才将Data解压成ARGB的图像,

所以在每次画图的时候,会有一个解压操作,这样效率很低,但是只有瞬时的内存需求。

为了提高效率通过SDWebImageDecoder将包装在Data下的资源解压,然后画在另外一张图片上,这样这张新图片就不再需要重复解压了。

这种做法是典型的空间换时间的做法。

首先NSURLCache是缓存原始数据(raw data)到磁盘或内存,因此每次使用的时候需要将原始数据转换成具体的对象,如UIImage等,这会导致额外的数据解析以及内存占用等,而SDWebImage则是缓存UIImage对象在内存,缓存在NSCache中,同时直接保存压缩过的图片到磁盘中;还有一个问题是当你第一次在UIImageView中使用image对象的时候,图片的解码是在主线程中运行的!而SDWebImage会强制将解码操作放到子线程中。

以上记录下来备忘 还不是很理解怎么回事

SDWebImage3.7.5源码阅读二中看完后,已经知道SDWebImageCombinedOperation类的在cacheOperation搜索完缓存之后:

1
2
3
4
5
6
7
8
9
10
11
12
operation.cacheOperation = [self.imageCache queryDiskCacheForKey:key done:^(UIImage *image, SDImageCacheType cacheType) {

...
operation.cacheOperation = [self.imageCache queryDiskCacheForKey:key done:^(UIImage *image, SDImageCacheType cacheType) {

...//一大串 options的操作 暂时不管它

//终于见到了下载操作!
id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed
...

}
阅读全文 »

3. downloadImageWithURL下载方法的具体实现

方法在SDWebImageManager.m中

1
2
3
4
id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url 	
options:(SDWebImageOptions)options
progress:(SDWebImageDownloaderProgressBlock)progressBlock
completed:(SDWebImageCompletionWithFinishedBlock)completedBlock

参数:

  • url
  • options
    • 之前已经介绍了 SDWebImageOptions
  • progressBlock
    • SDWebImageDownloaderProgressBlock 定义在 SDWebImageDownloader.h 中具体实现为:
      1
      2
      3
      //从名字可以看出来第一个参数是已经接受了数据的大小
      //另一个参数表示总数据的大小
      typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize);
  • completedBlock
    • 图片下载完要做的块 具体实现为:
      1
      2
      		
      typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL);

然后一看这个方法实现……麻蛋 好长!

阅读全文 »

使用System Sound Service播放音效

参考

步骤

  1. 调用AudioServicesCreateSystemSoundID( CFURLRef inFileURL, SystemSoundID* outSystemSoundID)函数获得系统声音ID。
  2. 如果需要监听播放完成操作,则使用AudioServicesAddSystemSoundCompletion( SystemSoundID inSystemSoundID, CFRunLoopRef inRunLoop, CFStringRef inRunLoopMode, AudioServicesSystemSoundCompletionProc inCompletionRoutine, void* inClientData)方法注册回调函数。
  3. 调用AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) 或者AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) 方法播放音效(后者带有震动效果)。
阅读全文 »

AVSpeechSynthesizer 文字转语音

简单示例:

1
2
3
4
5

AVSpeechSynthesizer *synthesizer = [AVSpeechSynthesizer new];
AVSpeechUtterance * utterance = [[AVSpeechUtterance alloc] initWithString:@"需要转化的文字"];
//语音合成器会生成音频
[synthesizer speakUtterance:utterance];

指定声音:

阅读全文 »

0. 图片的异步下载

比如在tableview中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString* cellID = @"cellID";

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
cell.textLabel.text = @" text ";

return cell;

}
阅读全文 »
0%