2014-09-29 31 views
0

我想显示一个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; 
} 

回答

0

我认为你只是在创建一个图像,最终,图像太大而不适合内存。而不是通过追加上一页的图像来创建一个更大更大的图像,也许你可以在上一页下面显示下一页的图像。

为webview,你总是可以生成一个新的“HTML页面”,将包含您所需的图像。使用函数像这样的:

- (NSString*)htmlStringWithImagesOnPaths:(NSArray*)imagePaths { 
    NSString* html = @"<html><head><title></title></head><body>"; 

    for(NSString* imagePath in imagePaths) 
     html = [html stringByAppendingString:[NSString stringWithFormat:@"<img src=\"%@\">", imagePath]]; 

    return [html stringByAppendingString:@"</body></html>"]; 
} 

然后代替[self.webView1 loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:self.tiffMainPageName]]]你会做:

NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0]; 
[webView loadHTMLString:[self htmlStringWithImagesOnPaths:self.imagePaths] baseURL:[NSURL fileURLWithPath: documentsDirectoryPath]]; 
在这种情况下

,应提供在htmlStringWithImagesOnPaths:方法的imagePaths参数相对路径的阵列(相对于文档目录)。

还有一点需要注意 - 我不知道为什么在NSArray *arrayOfBytes = [dicIn objectForKey:@"FileBytes"];之后,您要将此字节数组转换为NSString,然后再回到字节,然后将其保存到文件中。为什么不情愿将其保存到一个文件的时候了(使用[arrayOfBytes writeToFile:fileWithDirPath atomically:YES])

好运

+0

网络视图显示了问号,而不是图像。不工作的me.I'm试图表明从文件目录,而不是图像为什么基础url是用空字符串做的[webView loadHTMLString:[self htmlStringWithImagesOnPaths:self.imagePaths] baseURL:[NSURL URLWithString:@“”]];感谢您的帮助 – user1994956 2014-09-30 10:23:11

+0

@ user1994956好吧,它应该工作甚至对于存储在文档目录中的图像,可能图像URL不正确,如果你使用绝对URL,baseURL应该没有任何作用,所以我只是把一个空字符串(尽管可以为零)...可能更简单的方法如何实现这一点实际上会使用相对URL的图像和设置在baseURL - 我会编辑我的帖子。 – DaTa 2014-09-30 11:54:27

+0

@ user1994956有什么消息吗?即使使用所描述的技术,您的应用程序仍然崩溃?如果回答您的问题,请将帖子标记为答案。谢谢。 – DaTa 2014-10-01 23:49:43

相关问题