2
내가 성취 한 게으른 로딩을 스크롤 만드는 방법 :이 화면에 인에서 +/- 2 "페이지"인 경우 게으른로드있는 UIScrollView -이 같이있는 ScrollView에 부드러운
-(void)scrollViewDidScroll:(UIScrollView *)myScrollView {
int currentPage = (1 + myScrollView.contentOffset.x/kXItemSpacingIphone);
for (ItemView* itemView in [self.itemRow subviews]){
if (itemView.tag >= currentPage-2 && itemView.tag <= currentPage+2)
{
//keep it visible
if (!itemView.isLoaded) {
[itemView layoutWithData:[self.items objectAtIndex:itemView.tag-1]];
}
}
else
{
//hide it
if (itemView.isLoaded) {
[itemView unloadData];
}
}
}
}
는 기본적으로 뷰를로드. 이렇게하면 (20 개 이상의 ItemViews를 한 번에로드하는 대신) 사용하는 메모리의 양이 크게 줄어 듭니다. 그러나 모든로드/언로드는 특히 느린 장치에서 스크롤이 조금 고르지 않게 만듭니다. 여기에 실제로 ItemView 로딩에 무슨 일이 일어나고있다 :
- (void)layoutWithData:(Item*)_data {
self.data = _data;
//grab the image from the bundle
UIImage *img;
NSString *filePath = [[NSBundle mainBundle] pathForResource:_data.image ofType:@"jpg"];
if(filePath.length > 0 && filePath != (id)[NSNull null]) {
img = [UIImage imageWithContentsOfFile:filePath];
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:img forState:UIControlStateNormal];
[btn addTarget:self action:@selector(tapDetected:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(0, 0, kItemPosterWidthIphone, kItemPosterHeightIphone);
[self addSubview:btn];
self.isLoaded = YES;
}
그리고 ItemView 언로드 :
다시- (void)unloadData{
for(UIView *subview in [self subviews]) {
[subview removeFromSuperview];
}
self.data = nil;
self.isLoaded = NO;
}
, 내가 빨리 때문에있는 UIScrollView 더 부드럽게 언로드 /로드를 만들기 위해 무엇을 할 수 있는가?
시도 비동기은 :
- (void)layoutWithData:(Item*)_data {
self.data = _data;
self.isLoaded = YES;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
UIImage *img;
NSString *filePath = [[NSBundle mainBundle] pathForResource:_data.image ofType:@"jpg"];
if(filePath.length > 0 && filePath != (id)[NSNull null]) {
img = [UIImage imageWithContentsOfFile:filePath];
}
dispatch_async(dispatch_get_main_queue(), ^{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:img forState:UIControlStateNormal];
[btn addTarget:self action:@selector(tapDetected:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(0, 0, kItemPosterWidthIphone, kItemPosterHeightIphone);
self.imageView = btn;
[self addSubview:btn];
});
});
}
좋은 제안 : 이미지 로딩 시차도 UIImages
여기를 참조하십시오의 지연 처리에 의해 발생할 수 있습니다. 불행히도 오래된 장치에서는 여전히 뒤떨어져 있습니다. "비동기 시도 중"에서 편집 한 내용을 볼 수 있습니까? 그리고 내가 잘못했는지 확인하십시오. – soleil
더 느린 장치의 경우 화면에 그리는 것이 지연됩니다. 느린 장치의 축소판 이미지 (품질이 낮은 이미지라고도 함)를 사용하여 문제를 해결했습니다. 배경 스레드에서 이미지를 올바르게로드하고 있습니다. 이미지가 실제로 HD입니까? HD가되어야합니까? – yuf
안녕하세요,이 질문을 읽어주십시오 : http://stackoverflow.com/questions/10790183/setting-image-property-of-uiimageview-causes-major-lag 문제가 될 수 있습니다. – yuf