LeavesCache.m works on simulator but not on device

2019-09-06 02:33发布

问题:

Its my first time posting so please bear with me. I have been searching sites after sites and I can't seem to find any which has resolved this issue. Would be happy if someone could answer me.

LeavesCache.m was modified last year if not mistaken to accommodate ios 7 i think.

The code is below however I receive a EXC_BAD_ACCESS for return pageImage.CGImage.

I changed it to return (__bridge CGImageRef)(pageImage) but now my page comes out blank even though it prints the correct page index. Please help!

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);
}

On a quick note, the content comes out perfectly on the simulator but blank on the iPad.

回答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;
}