AFNetworking + Many imageviews = Crash -> Received memory warning

╄→尐↘猪︶ㄣ 提交于 2019-12-25 04:31:13

问题


I 've never had problems with my app using AFNetworking, because I had like 20 imageview to show , but now my app crash because I want to set imagewithURL to 150 Imageviews, if I commented that line all is ok, this is my code:

for (int i=0; i< Array.count; i++) {
  UIImageView *imgProd=[[UIImageView alloc] initWithFrame:CGRectMake(margenX, margenY, 220, 330)];
  imgProd.contentMode = UIViewContentModeScaleAspectFill;
  imgProd.clipsToBounds = YES;
  [imgProd setTag:i];
  // dispatch_async(dispatch_get_main_queue(), ^{
  [imgProd setImageWithURL:[NSURL URLWithString: [Array objectAtIndex:i]]];
  // });
  imgProd.userInteractionEnabled = YES;
  UITapGestureRecognizer *tap ... etc etc.
}

I put a dispatch_async, but is the same problem, please some advices!, thanks :)


回答1:


You are creating 150 UIImageViews and are filling them up with imagedata which mess up your memory and crashes your app.

You should make a UITableView or UICollectionView and use the built in memory handler

dequeueReusableCellWithReuseIdentifier

, to show the images on the screen and reuse only one and the same UIImageView in the cell. Not create 150 uiimageviews that is, only one is needed.

Some fast googling got me to this tutorial: https://www.youtube.com/watch?v=OBL8OJUWmsI

Gl !



来源:https://stackoverflow.com/questions/30514038/afnetworking-many-imageviews-crash-received-memory-warning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!