iphone - UIImage problem -
i loading images of size 450kb in uiimage view , adding uiscrollview. while scrolling 30 images continously,its getting crashed.. may reason..is memory leak issue...or image size problem...? in advance..
here code ..
@try{ nsautoreleasepool *pool; pool = [[nsautoreleasepool alloc] init]; //nsarray *array = [global_contentstring componentsseparatedbystring:@"@@#"]; nsarray *array1 = [catalogurl componentsseparatedbystring:@"&"]; //**nslog(@"array1****** = %@",array1); nslog(@"loading catalog image(method: loadcatalogimage).......%@%@",baseurl, [[[array1 objectatindex:0] componentsseparatedbystring:@"##"] objectatindex:0]); //nslog(@"baseurl = %@",baseurl); nslog(@"loading catalog image.......%@%@",baseurl, [[[array1 objectatindex:0] componentsseparatedbystring:@"##"] objectatindex:0]); zoomedimageurl = [nsstring stringwithformat:@"%@%@", baseurl, [[[array1 objectatindex:0] componentsseparatedbystring:@"##"] objectatindex:1]]; [zoomedimageurl retain]; nslog(@"aaaaaaaaaaaaaa = %@",zoomedimageurl); //uiimage *img = [uiimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:[nsstring stringwithformat:@"%@%@",baseurl, [[[array1 objectatindex:0] componentsseparatedbystring:@"##"] objectatindex:0]]]]]; uiimage *img = [uiimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:[nsstring stringwithformat:@"%@",zoomedimageurl]]]]; imgview.contentmode = uiviewcontentmodescaleaspectfit; imgview.image = img;//[gpstriptracking generatephotothumbnail:img:109]; [pool release]; [global_imgprogress stopanimating]; } @catch (nsexception *e) { [global_imgprogress stopanimating]; nslog(@"exception...."); } @finally { }
am releasing imgview in dealloc method..
i imlemented following code in "scrollviewdidscroll"
- (void)scrollviewdidscroll:(uiscrollview *)sender { // don't want "feedback loop" between uipagecontrol , scroll delegate in // scroll event generated user hitting page control triggers updates // delegate method. use boolean disable delegate logic when page control used. if (pagecontrolused) { // nothing - scroll initiated page control, not user dragging //pagetext.text = [nsstring stringwithformat:@"%d/%d", (pagecontrol.currentpage +1), pagecontrol.numberofpages]; pagetext.text = [nsstring stringwithformat:@"%d/%d", (pagecontrol.currentpage ), pagecontrol.numberofpages]; //nslog(@"not scrollling page...."); return; } // switch indicator when more 50% of previous/next page visible cgfloat pagewidth = scrollview.frame.size.width; int page = floor((scrollview.contentoffset.x - pagewidth / 2) / pagewidth) + 1; pagecontrol.currentpage = page; // load visible page , page on either side of (to avoid flashes when user starts scrolling) [self loadscrollviewwithpage:page - 1]; [self loadscrollviewwithpage:page]; [self loadscrollviewwithpage:page + 1]; //nslog(@"scrolling page....%d", page); // possible optimization unload views+controllers no longer visible
}
and code " loadscrollviewwithpage" is
- (void)loadscrollviewwithpage:(int)page
{ //page--;
if (page < 0) return; if (page >= numberofpages) return; if(!isviewcatalog && searchid == 1) { //nslog(@"curre page = %d",pagecontrol.currentpage); nsarray *array1 = [global_contentstring componentsseparatedbystring:@"@@#"]; if(searchincatalogflag == 1) { pagecontrol.currentpage=0; nsarray *urlarray = [[array1 objectatindex:pagecontrol.currentpage] componentsseparatedbystring:@"##"]; //nslog(@"url array** = %@",urlarray); headertext.text = [nsstring stringwithstring:[urlarray objectatindex:0]]; pagetext.text = [nsstring stringwithformat:@"%d/%d", pagecontrol.currentpage, (pagecontrol.numberofpages - 1)]; } else { nsarray *urlarray = [[array1 objectatindex:pagecontrol.currentpage] componentsseparatedbystring:@"##"]; //nslog(@"url array** = %@",urlarray); headertext.text = [nsstring stringwithstring:[urlarray objectatindex:0]]; pagetext.text = [nsstring stringwithformat:@"%d/%d", pagecontrol.currentpage, (pagecontrol.numberofpages - 1)]; } if(page == selectedpage && ![global_imgprogress isanimating]) [global_imgprogress startanimating]; } else { headertext.text = [nsstring stringwithstring:global_selectedcatalogname]; pagetext.text = [nsstring stringwithformat:@"%d/%d", (pagecontrol.currentpage + 1), (pagecontrol.numberofpages - 1)]; if(page == selectedpage + 1 && ![global_imgprogress isanimating] ) [global_imgprogress startanimating]; // nslog(@"header text = %@", headertext.text); //headertext.text = [nsstring stringwithstring:[urlarray objectatindex:0]]; } fullpageview *controller = [viewcontrollers objectatindex:page]; if ((nsnull *)controller == [nsnull null] ) { //nslog(@"loading page =========== %d, %d", page, selectedpage); //voucherpageno = page; //[voucherimage retain]; if(universalapp==2) { controller = [[fullpageview alloc] initwithnibname:@"fullpageview_iphone" bundle:nil];//:page]; [controller.view setframe:cgrectmake(0, 0, 320,332)]; } else { controller = [[fullpageview alloc] initwithnibname:@"fullpageview" bundle:nil];//:page]; [controller.view setframe:cgrectmake(0, 192, 768, 691)]; } //[controller.view setframe:cgrectmake(0, 0, 320,480)]; //[controller.view setframe:cgrectmake(0, 192, 768, 691)]; if((!isviewcatalog && searchid < 2 && searchincatalogflag == 0))// || searchincatalogflag == 1) { // nslog(@">>>>>>>>>>>>>>>>>> loading image >>>>>>>>>>>>>>>>>>>>"); [controller setpageno:page]; // if(page >= selectedpage - 1) [nsthread detachnewthreadselector:@selector(loadimage) totarget:controller withobject:nil]; } else //if((page >= (selectedpage - 1) && page <= (selectedpage + 1)) || !isfirsttimeloading) { nslog(@"loading catalog image = %d, %d, %@", page, selectedpage, (isfirsttimeloading ?@"yes" : @"no")); [controller setcatalogurl:[nsstring stringwithformat:@"%@", [catalogarray objectatindex:page+(searchid< 2 && !isviewcatalog && searchincatalogflag == 0?0:1)]]]; nslog(@"loading image ipad= %@", [catalogarray objectatindex:page+(searchid< 2 && !isviewcatalog && searchincatalogflag == 0?0:1)]); // if(page >= selectedpage - 1) [nsthread detachnewthreadselector:@selector(loadcatalogimage) totarget:controller withobject:nil]; // if(page == (selectedpage + 1)) //isfirsttimeloading = no; } [viewcontrollers replaceobjectatindex:page withobject:controller]; [controller release]; } // add controller's view scroll view if (nil == controller.view.superview) { // nslog(@"voucher view addead @ page..... %d", page); cgrect frame = scrollview.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; controller.view.frame = frame; // nslog(@">>>>>>>>> %f, %f", frame.size.width, frame.origin.x); [scrollview addsubview:controller.view]; } //if(page == pagecontrol.currentpage) //[imgprogress startanimating]; //else //pagecontrolused = yes;
} wil problem..?
it program gets terminated ios because consumes memory. start xcode , @ console - print receives memory warnings.
you have load images on demand, i.e. when user gets close seeing them, , have release ones move out of view again. end, implement the
- (void)scrollviewdidscroll:(uiscrollview *)scrollview
delegate method, @ scroll view's content offset , load/release appropriate images. alternatively, can choose not release them there instead wait memory warning so. so, implement the
- (void)didreceivememorywarning
method in view controller.
Comments
Post a Comment