簡體   English   中英

iPhone應用程序在附加圖像並加載到uiwebview中時會占用大量內存

[英]iPhone app take a lot of memory while appending images and loading in uiwebview

我想顯示一個tiff文件,可以說25頁。 我必須通過滾動逐頁顯示tiff文件。 僅供參考,我不想使用來自github的NSTiffSplitter,我想下載下一頁,因為用戶向下滾動uiwebview的scrollview。 我正在下載第一頁字節(數組),將其轉換為.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;
}

我認為您只是在創建一個最終太大而無法容納到內存中的圖像。 而是通過附加上一頁的圖像來創建越來越大的圖像,可能只是您可以在上一頁的下方顯示下一頁的圖像。

對於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:方法(相對於documents目錄)的imagePaths參數中提供一個相對路徑數組。

另一注-我不確定為什么在NSArray *arrayOfBytes = [dicIn objectForKey:@"FileBytes"]; 您需要將此字節數組轉換為NSString ,然后再轉換回字節,然后將其保存到文件中。 為什么不立即將其保存到文件中(使用[arrayOfBytes writeToFile:fileWithDirPath atomically:YES])

祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM