[英]ScrollView a lot of images which use a lot of memory
My app for the iPhone Contains 170 images. 我的iPhone应用程序包含170张图像。 I read them into an array, and in the loop add a picture in the viewfinder, then put the image View as sub view of scroll view. 我将它们读入数组,然后在循环中在取景器中添加图片,然后将图像“ View”作为滚动视图的子视图。
When running my app uses too much memory 700mb. 运行我的应用程序时,会占用太多内存700mb。 I tried to reduce the size of the pictures, but it didn't work. 我试图缩小图片的尺寸,但是没有用。
One of my friends said that I should add only images # 1 and # 2. When the user block is scrolled to the picture No. 1, then only show the picture No. 2. Then the picture No. 1 to remove from the image viewer and add the picture No. 3. 我的一位朋友说,我应该只添加图像#1和#2。当用户块滚动到图片1时,然后仅显示图片2。然后从图片中删除图片1。查看器并添加图片3。
He says that in this way I can maintain the normal memory consumption. 他说,这样可以保持正常的内存消耗。 But I don't understand how to do this? 但是我不知道该怎么做? : D :D
Could you help with an example? 你能帮个例子吗? Thanks in advance. 提前致谢。
Not using UICollectionView 不使用UICollectionView
Here is my code: 这是我的代码:
- (void) addImageViewToScrollView {
//create ArrayImages
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int x = 0; x < 170; x++) {
[tempArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",x]]];
}
self.imagesArray = [NSArray arrayWithArray:tempArray];
//Frame ScrollView in UIView
self.scrollView.contentSize =
CGSizeMake(self.scrollView.frame.size.width * self.imagesArray.count,
self.scrollView.frame.size.height);
//scrollView add subview ImageView
for (int i = 0; i < self.imagesArray.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
_imageView = [[UIImageView alloc] init];
_imageView.image = [self.imagesArray objectAtIndex:i];
_imageView.frame = frame;
[scrollView addSubview:_imageView];
}
self.pageControll.currentPage = 0;
self.pageControll.numberOfPages = self.imagesArray.count;
}
I decided to split the code for readability 我决定拆分代码以提高可读性
- (void)scrollViewDidScroll:(UIScrollView *)sender {
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControll.currentPage = page;
self.imageIndex = page;
}
#pragma mark - ChangePage
- (IBAction)changePage {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControll.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
You can use imageWithContentsOfFile instead of using imageNamed as imageWithContentsOfFile doesn't cache the images. 您可以使用imageWithContentsOfFile而不是使用imageNamed,因为imageWithContentsOfFile不会缓存图像。 Hence preferable to use, to reduce memory issues. 因此最好使用,以减少内存问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.