iPhone应用程序在附加图像和加载到uiwebview时占用大量内存

时间:2014-09-29 19:48:12

标签: iphone image memory uiwebview crash

我想显示一个tiff文件,可以说25页。我必须通过滚动逐页显示tiff文件。仅供参考,我不想在github上使用NSTiffSplitter,我想在用户向下滚动uiwebview的scrollview时下载下一页。 我正在下载第一页字节(数组),将其转换为.jpeg文件(image1.jpeg)并将其写入文档目录并将此下载的页面加载到UIWebview中。现在当用户在scrollview的末尾向下滑动时,我将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;
}

1 个答案:

答案 0 :(得分: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]];

在这种情况下,您应该在imagePaths方法的htmlStringWithImagesOnPaths:参数中提供一个相对路径数组(相对于文档目录)。

还有一个注意事项 - 我不确定为什么在NSArray *arrayOfBytes = [dicIn objectForKey:@"FileBytes"];之后你将这个字节数组转换为NSString然后再转换为字节,然后将其保存到文件中。为什么不立即将它保存到文件中(使用[arrayOfBytes writeToFile:fileWithDirPath atomically:YES])

祝你好运