【发布时间】:2014-11-24 07:21:43
【问题描述】:
我想显示一个 tiff 文件,比如说 25 页。我必须通过滚动逐页显示 tiff 文件。仅供参考,我不想使用来自 github 的 NSTiffSplitter,我想在用户向下滚动 uiwebview 的滚动视图时下载下一页。 我正在下载第一页字节(数组),将其转换为 .jpeg 文件(image1.jpeg)并将其写入文档目录并将下载的页面加载到 UIWebview 中。现在,当用户在滚动视图结束时向下滑动时,我将 tiff 文件的第二页下载为字节,将其转换为 .jpeg(image2.jpeg),将其保存到文档目录并附加第一页(image1.jpeg),然后将附加的图像加载到 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;
}
【问题讨论】:
标签: iphone image memory uiwebview crash