但在设备上不工作

但在设备上不工作

本文介绍了LeavesCache.m 在模拟器上工作,但在设备上不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次发帖,所以请多多包涵.我一直在一个接一个地搜索站点,但似乎找不到解决此问题的任何站点.如果有人能回答我会很高兴.

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 是在去年修改的,我认为是为了适应 ios 7.

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

代码如下,但是我收到一个 EXC_BAD_ACCESS 用于 return pageImage.CGImage.

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

我将其更改为 return (__bridge CGImageRef)(pageImage) 但现在我的页面显示为空白,即使它打印了正确的页面索引.请帮忙!

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

简而言之,内容在模拟器上完美呈现,但在 iPad 上却是空白.

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

推荐答案

我终于设法解决了这个问题.我不知道这有多合适.但是对于那些使用 LeavesView 的人.如果'return pageImage.CGImage' 抛出'EXC_BAD_ACCESS',你可以通过删除这一行来解决它:

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

在这个函数中找到,'imageForPageIndex':

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

请维护您的 'cachedImageForPageIndex:' 函数.如果改了,代码如下:

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 在模拟器上工作,但在设备上不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:34