我想顯示一個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