-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view, typically from a nib.NSString*filePath=[[NSBundlemainBundle]pathForResource:@"qq_head"ofType:@"jpg"];[selfloadImageFile:filePath];}#pragma mark---------NSStreamDelegate-----------(void)stream:(NSStream*)aStreamhandleEvent:(NSStreamEvent)eventCode{constNSUIntegerkBufferSize=2048;if(aStream==_imageInputStream){NSLog(@"eventcode = %u",eventCode);switch(eventCode){caseNSStreamEventHasBytesAvailable:{uint8_tbuf[kBufferSize];memset(buf,0,kBufferSize*sizeof(uint8_t));NSIntegernumOfBytes=[(NSInputStream*)aStreamread:bufmaxLength:kBufferSize];if(numOfBytes>0){[_imageOutputStreamwrite:(constvoid*)bufmaxLength:numOfBytes];}break;}caseNSStreamEventEndEncountered:{NSLog(@"The end of the stream has been reached.");NSData*imageData=[_imageOutputStreampropertyForKey:NSStreamDataWrittenToMemoryStreamKey];if(imageData.length>0){UIImage*icon=[[UIImagealloc]initWithData:imageData];_imageView.image=icon;}[selfcleanUpStream];break;}caseNSStreamEventErrorOccurred:{NSLog(@"NSStreamEventErrorOccurred!!");[selfcleanUpStream];}default:break;}}}-(IBAction)loadNewImageFileEvnet:(id)sender{NSString*filePath=[[NSBundlemainBundle]pathForResource:@"qq_head_1"ofType:@"jpg"];[selfloadImageFile:filePath];}/** * 从本地图片文件中初始化输入流,同时初始化输出流。 * * @param fileName 图片文件全路径 */-(void)loadImageFile:(NSString*)fileName{if([[NSFileManagerdefaultManager]fileExistsAtPath:fileName]){self.imageInputStream=[NSInputStreaminputStreamWithFileAtPath:fileName];_imageInputStream.delegate=self;[_imageInputStreamscheduleInRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSDefaultRunLoopMode];[_imageInputStreamopen];self.imageOutputStream=[NSOutputStreamoutputStreamToMemory];[_imageOutputStreamopen];}else{NSLog(@"%@ ---not found the file",fileName);}}/** * 清理输出输入流 */-(void)cleanUpStream{[_imageInputStreamremoveFromRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSDefaultRunLoopMode];_imageInputStream.delegate=nil;[_imageInputStreamclose];_imageInputStream=nil;[_imageOutputStreamclose];_imageOutputStream=nil;}