我想显示一个tiff文件可以说25页。我必须通过滚动来逐页显示tiff文件。仅供参考,我不想使用github上的NSTiffSplitter,我想下载下一页作为用户向下滚动uiwebview的滚动视图。 我正在下载第一页字节(数组),将其转换成.jpeg文件(image1.jpeg)并将其写入文档目录并将此下载的页面加载到UIWebview中。现在,当用户在滚动视图的末尾向下滑动时,我将tiff文件的第二页作为字节下载,将其转换为.jpeg(image2.jpeg),将其保存到文档目录中并将其附加到第一页(image1.jpeg),然后将附加图像加载到UIWebview。iPhone应用程序需要大量的内存,同时在uiwebview中添加图像和加载
这适用于可以说7页,8至10应用程序显示内存警告和崩溃。让我发布代码。
我的想法是,imageByCombiningImage是吃内存,而不是释放它。需要专家提醒。
-(void)saveAndDisplayFullImageiPad:(NSDictionary *)dicIn{
int pageNumber = [[dicIn objectForKey:@"SortNo"]intValue];//page number comes in web service response
self.currentFilePageCount = [[dicIn objectForKey:@"PagesCount"]intValue];
NSArray *arrayOfBytes = [dicIn objectForKey:@"FileBytes"];
if (arrayOfBytes != nil && [arrayOfBytes count] > 0) {
unsigned c = arrayOfBytes.count;
uint8_t *bytes = malloc(sizeof(*bytes) * c);
unsigned i;
for (i = 0; i < c; i++){
NSString *str = [arrayOfBytes objectAtIndex:i];
int byte = [str intValue];
bytes[i] = (uint8_t)byte;
}
NSData *data = [NSData dataWithBytes:(const void *)bytes length:sizeof(unsigned char)*c];
free(bytes);
NSString *fileNameCPath=[dicIn objectForKey:@"FilePath"];
NSString *fileNamePlusExtension = [[fileNameCPath componentsSeparatedByString:@"\\"] lastObject];
NSString *justFileName = [[fileNamePlusExtension componentsSeparatedByString:@"." ]firstObject];
self.standardTiffPageName = [justFileName stringByAppendingString:@"PageNumber"];
NSString *fileWithPageNum = [justFileName stringByAppendingString:[NSString stringWithFormat:@"PageNumber%d.jpeg",pageNumber]];
NSString *fileWithDirPath = [[[AppSettings sharedAppSettings]getDocumentDirectory] stringByAppendingPathComponent:fileWithPageNum];//document directory path appended
NSLog(@"filePathWritten = %@",fileWithDirPath);
[data writeToFile:fileWithDirPath atomically:YES];
self.tiffMainPageName = fileWithDirPath;
[self.tiffPagesNameArray addObject:fileWithDirPath];//just saving pages path names
if (pageNumber == 1) {
self.tiffMainPageName = fileWithDirPath;//tiffMainPageName has the appended images
}
if (pageNumber > 1) {
UIImage *img1 = [UIImage imageWithContentsOfFile:self.tiffMainPageName];
UIImage *img2 = [UIImage imageWithContentsOfFile:fileWithDirPath];
UIImage *finalImage = [self imageByCombiningImage:img1 withImage:img2];
NSData *dataFinalImage = UIImageJPEGRepresentation(finalImage, 0.5);
[dataFinalImage writeToFile:self.tiffMainPageName atomically:YES];
}
}
[self.webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:self.tiffMainPageName]]];
self.tiffPageDownloadComplete = YES;
}
- (UIImage*)imageByCombiningImage:(UIImage*)image1 withImage:(UIImage*)image2 {
CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);
UIGraphicsBeginImageContext(size);
[image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}
网络视图显示了问号,而不是图像。不工作的me.I'm试图表明从文件目录,而不是图像为什么基础url是用空字符串做的[webView loadHTMLString:[self htmlStringWithImagesOnPaths:self.imagePaths] baseURL:[NSURL URLWithString:@“”]];感谢您的帮助 – user1994956 2014-09-30 10:23:11
@ user1994956好吧,它应该工作甚至对于存储在文档目录中的图像,可能图像URL不正确,如果你使用绝对URL,baseURL应该没有任何作用,所以我只是把一个空字符串(尽管可以为零)...可能更简单的方法如何实现这一点实际上会使用相对URL的图像和设置在baseURL - 我会编辑我的帖子。 – DaTa 2014-09-30 11:54:27
@ user1994956有什么消息吗?即使使用所描述的技术,您的应用程序仍然崩溃?如果回答您的问题,请将帖子标记为答案。谢谢。 – DaTa 2014-10-01 23:49:43