LeavesCache.m工作在模拟器,但不是在装置(LeavesCache.m works on

2019-11-03 11:21发布

它的我第一次发贴,请多多包涵。 我一直在寻找的网站后,网站和我似乎无法找到任何已经解决了这个问题。 会很高兴,如果有人能够回答我。

LeavesCache.m去年修改如果没记错,以适应IOS 7,我认为。

代码如下,但是我收到一个EXC_BAD_ACCESSreturn pageImage.CGImage

我改成了return (__bridge CGImageRef)(pageImage)但现在我的网页出来空白,即使它打印正确的页面索引。 请帮忙!

CGImageRef)cachedImageForPageIndex:(NSUInteger)pageIndex {
NSNumber *pageIndexNumber = [NSNumber numberWithInt:pageIndex];
UIImage *pageImage;
@synchronized (self.pageCache) {
    pageImage = [self.pageCache objectForKey:pageIndexNumber];
}
if (!pageImage) {
    CGImageRef pageCGImage = CGImageRetain([self imageForPageIndex:pageIndex]);

    if (pageCGImage) {

        pageImage = [UIImage imageWithCGImage:pageCGImage];            
        @synchronized (self.pageCache) {
            [self.pageCache setObject:pageImage forKey:pageIndexNumber];
        }
    }
}

//modified here
//return pageImage.CGImage;
return (__bridge CGImageRef)(pageImage);
}

在一个快速的注意,内容出来完美的模拟器,但在iPad上的空白。

Answer 1:

I managed to finally resolve this. How proper is this I don't know. But to those using LeavesView. If 'return pageImage.CGImage' is throwing you 'EXC_BAD_ACCESS', you can resolve it by removing this line:

//CGImageRelease(image);

It is found in this function, 'imageForPageIndex':

- (CGImageRef)imageForPageIndex:(NSUInteger)pageIndex {
if (CGSizeEqualToSize(self.pageSize, CGSizeZero))
    return NULL;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
                                             self.pageSize.width,
                                             self.pageSize.height,
                                             8,                     /* bits per component*/
                                             self.pageSize.width * 4,   /* bytes per row */
                                             colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextClipToRect(context, CGRectMake(0, 0, self.pageSize.width, self.pageSize.height));

[self.dataSource renderPageAtIndex:pageIndex inContext:context];

CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);

[UIImage imageWithCGImage:image];
//CGImageRelease(image);

return image;
}

Please maintain your 'cachedImageForPageIndex:' function. If you have changed it, the code is as follows:

- (CGImageRef)cachedImageForPageIndex:(NSUInteger)pageIndex {
NSNumber *pageIndexNumber = [NSNumber numberWithInt:pageIndex];
UIImage *pageImage;
@synchronized (self.pageCache) {
    pageImage = [self.pageCache objectForKey:pageIndexNumber];
}
if (!pageImage) {
    CGImageRef pageCGImage = [self imageForPageIndex:pageIndex];
    if (pageCGImage) {
        pageImage = [UIImage imageWithCGImage:pageCGImage];
        @synchronized (self.pageCache) {
            [self.pageCache setObject:pageImage forKey:pageIndexNumber];
        }
    }
}
return pageImage.CGImage;
}


文章来源: LeavesCache.m works on simulator but not on device