UIScrollView doesn't release images - ios

I have a UIScrollView that shows a gallery of images from a plist file.
I also have a function to delete an image from the gallery image list that basically deletes an object in the plist file and then reload the images in the ScrollView.
The issue is I am not able to release the images of the UIScrollView before to reload it with the new content when I use the method - (IBAction)deleteimage:(id)sender. The new content is loaded but over the older content and then the images are still behind the new one.
What I should do to release images before to reload the scrollview content?
The code I am using is :
#import "ImageScrollViewController.h"
#interface ImageScrollViewController ()
#end
#implementation ImageScrollViewController
#synthesize images,scrollView,pageControl,subview;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self setupthescroll];
self.pageControl.currentPage = 0;
}
- (void)setupthescroll{
//Get the images of the Array
NSUserDefaults *success = [NSUserDefaults standardUserDefaults];
images = [success mutableArrayValueForKey:#"imagelist"];
NSLog(#"list of images%#",images);
pageControlBeingUsed = NO;
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
NSString *str4 = [images objectAtIndex:i];
subview.image = [[[UIImage alloc] initWithContentsOfFile:str4] autorelease];
self.subview.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = images.count;
//Get the number of the images
int page;
page = self.pageControl.currentPage;
printf("Current Page: %d", page);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (IBAction)changePage {
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
- (IBAction)deleteimage:(id)sender{
//Get the number of the image
int page;
page = self.pageControl.currentPage;
printf("Current Page: %d", page);
//Remove the images of the Array
NSUserDefaults *success = [NSUserDefaults standardUserDefaults];
images = [success mutableArrayValueForKey:#"imagelist"];
[images removeObjectAtIndex:page];
NSLog(#"list of images%#",images);
//Update the Array
NSUserDefaults *arrayofimages = [NSUserDefaults standardUserDefaults];
[arrayofimages setObject:images forKey:#"imagelist"];
//Refresh the ScrollView
[self setupthescroll];
//post the notification than images have been updated
NSUserDefaults *deleted = [NSUserDefaults standardUserDefaults];
[deleted setObject:#"deleted" forKey:#"deletedimages"];
}
- (IBAction)closepage:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[pageControl release];
[scrollView release];
//[images release];
[super dealloc];
}
#end

If you are reloading the entire scrollview (i.e. adding all the images back), you first need to remove the existing images.
Use UIView's removeFromSuperview method to remove views from the scrollview.
So a code snippet to remove all image views from the scrollview would look something like this
NSArray *imgViews = [scrollView subviews];
for(id aView in imgViews){
if([aView isKindOfClass:[UIImageView class]]){
[aView removeFromSuperview]; //remove only if its an imageview
}
}
If you already have reference to the image views, you can directly call the method on them without iterating through all subviews of the scrollview.

You should remove all the UIImageView within the scrollView.
something like
NSArray* subviews = [[scrollview subviews] copy];
for (UIView* v in subviews) {
[v removeFromSuperview];
}
[subviews release];

You forgot to remove the previous image views from the scrollView subviews. A brutal method to to this (at least for testing the hypothesis) would be to add the following line at the beginning of setupthescroll:
[self.scrollView.subviews makeObjectsPerfomSelector:#selector(removeFromSuperview)];
The problem with this would be that the scrollview's private subviews ( scrollers, for instance ) would be removed too.
So in practice you should keep track of the subviews you created in an ivar array, and perform the above line on this array instead of the subviews array, and clear the array afterwards.
Alternatively, a much cleaner method would be to delete only the subview corresponding to the deleted image, and to update the frames of the remaining image views, instead of deleting and recreating everything. You could use a dictionary or the subviews tag property to keep track of wich view is associated to which image.

Related

Reload UIScrollView by tapping a UIButton

Let me explain my project first. I have some data in my SQLIte DB table called "note".
In "note" table I have these fields: id, noteToken, note.
What I am doing here is load all the note in an NSMUtableArray from that table. And create UIButton according to that array content number and add those buttons in a UIScrollView as subView. The number of buttons and width of scrollview generate auto according to the number of content of that array. Now, when some one tap one of those Buttons, it will bring him to a next viewController and show him the corresponding note details in that viewController.
I do the same thing with another NSMUtableArray, but these time it read all the id from the "note" table. It equally generate new delete button in the same UIScrollView. But if some one tap on these delete button it will delete that particular note from the table "note" of SQLIte DB. AND RELOAD THE UIScrollView. All are done except the RELOAD THE UIScrollView part. This is what I want. I tried with all exist solution but don't know why it's not working.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.noteToken = [NSString stringWithFormat:#"%#%#", fairId, exibitorId];
scrollViewNoteWidth = 100;
[scrollViewNote setScrollEnabled:YES];
[scrollViewNote setContentSize:CGSizeMake((noteButtonWidth * countNoteButtonArray) + scrollViewNoteWidth, 100)];
sqLite = [[SQLite alloc] init];
[self.sqLite callDataBaseAndNoteTableMethods];
self.noteButtonArrayy = [[NSMutableArray alloc] init];
noteButtonArrayy = [self.sqLite returnDataFromNoteTable:noteToken];
[self LoadNoteButtonAndDeleteButton:noteButtonArrayy];
}
//////////////*----------------------- Note Section (Down) -----------------------*//////////////
-(void) LoadNoteButtonAndDeleteButton:(NSMutableArray *) noteButtonArray
{
sQLiteClass = [[SQLiteClass alloc] init];
noteButtonArrayToShowNoteButton = [[NSMutableArray alloc] init];
/*--------------- Load the noteButton & pass note (Down)---------------*/
for (int i = 0; i < [noteButtonArray count]; i++)
{
sQLiteClass = [noteButtonArray objectAtIndex:i];
// NSString *ids = [NSString stringWithFormat:#"%d", sQLiteClass.idNum];
NSString *nt = sQLiteClass.note;
[noteButtonArrayToShowNoteButton addObject:nt];
}
[self ShowNoteButtonMethod:noteButtonArrayToShowNoteButton];
/*--------------- Load the noteButton & pass note (Up)---------------*/
/*--------------- Load the deleteButton & pass id (Down)---------------*/
noteButtonArrayToDeleteNoteButton = [[NSMutableArray alloc] init];
for (int i = 0; i < [noteButtonArray count]; i++)
{
sQLiteClass = [noteButtonArray objectAtIndex:i];
// Convert int into NSString
NSString *ids = [NSString stringWithFormat:#"%d", sQLiteClass.idNum];
[noteButtonArrayToDeleteNoteButton addObject:ids];
}
[self ShowNoteDeleteButtonMethod:noteButtonArrayToDeleteNoteButton];
/*--------------- Load the deleteButton & pass id (Down)---------------*/
}
-(void) ShowNoteButtonMethod:(NSMutableArray *) btnarray
{
countNoteButtonArray = [btnarray count];
// For note button
noteButtonWidth = 60;
noteButtonXposition = 8;
for (NSString *urls in btnarray)
{
noteButtonXposition = [self addNoteButton:noteButtonXposition AndURL:urls];
}
}
-(int) addNoteButton:(int) xposition AndURL:(NSString *) urls
{
noteButton =[ButtonClass buttonWithType:UIButtonTypeCustom];
noteButton.frame = CGRectMake(noteButtonXposition, 8.0, noteButtonWidth, 60.0);
[noteButton setImage:[UIImage imageNamed:#"note.png"] forState:UIControlStateNormal];
[noteButton addTarget:self action:#selector(tapOnNoteButton:) forControlEvents:UIControlEventTouchUpInside];
[noteButton setUrl:urls];
noteButton.backgroundColor = [UIColor clearColor];
[self.scrollViewNote addSubview:noteButton];
noteButtonXposition = noteButtonXposition + noteButtonWidth + 18;
return noteButtonXposition;
}
-(void)tapOnNoteButton:(ButtonClass*)sender
{
urlNote = sender.url;
[self performSegueWithIdentifier:#"goToNoteDetailsViewController" sender:urlNote];
}
-(void) ShowNoteDeleteButtonMethod:(NSMutableArray *) btnarray
{
countNoteButtonArray = [btnarray count];
// For delete button
deleteNoteButtonWidth = 14;
deleteNoteButtonXposition = 31;
for (NSString *idNumber in btnarray)
{
deleteNoteButtonXposition = [self addDeleteButton:deleteNoteButtonXposition AndURL:idNumber];
}
}
-(int) addDeleteButton:(int) xposition AndURL:(NSString *) idNumber
{
deleteNoteButton =[ButtonClass buttonWithType:UIButtonTypeCustom];
deleteNoteButton.frame = CGRectMake(deleteNoteButtonXposition, 74.0, deleteNoteButtonWidth, 20.0);
[deleteNoteButton setImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
[deleteNoteButton addTarget:self action:#selector(tapOnDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
[deleteNoteButton setIdNum:idNumber];
deleteNoteButton.backgroundColor = [UIColor clearColor];
[self.scrollViewNote addSubview:deleteNoteButton];
deleteNoteButtonXposition = deleteNoteButtonXposition + deleteNoteButtonWidth + 65;
return deleteNoteButtonXposition;
}
-(void)tapOnDeleteButton:(ButtonClass*)sender
{
idNumb = sender.idNum;
[self.sqLite deleteData:[NSString stringWithFormat:#"DELETE FROM note WHERE id IS '%#'", idNumb]];
// NSLog(#"idNumb %#", idNumb);
//[self.view setNeedsDisplay];
//[self.view setNeedsLayout];
//[self LoadNoteButtonAndDeleteButton];
//[self viewDidLoad];
// if ([self isViewLoaded])
// {
// //self.view = Nil;
// //[self viewDidLoad];
// [self LoadNoteButtonAndDeleteButton];
// }
}
//////////////*----------------------- Note Section (Up) -----------------------*//////////////
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"goToNoteDetailsViewController"])
{
NoteDetailsViewController *noteDetailsViewController = [segue destinationViewController];
[noteDetailsViewController setUrl:sender];
}
}
Here's the screen shot:
Here we can feel the difference between UIScrollView and UICollectionView, however UICollectionView is made up of UIScrollView, UICollectionView can be reload and adjust its content accordingly, where UIScrollView can't.
Ok, now in your case, you've to reload (refresh) your scroll view, which is not possible as we can with UICollectionView or UITableView.
You've two options,
Best option (little tough) : replace UIScrollView with UICollectionView - will take some of your time, but better for reducing code complexity and good performance of your app.
Poor option (easy) : Stay as it with UIScrollView - when you want to reload, delete each subview from it, and then again show and load everything. Highly not recommended.
IMHO, you should go with best option.

Load images on a image view using horizontal scrolling

I'm loading images on the UIImageView through a string and i want horizontal scrolling to view the images on a UIImageView.
In my xib file I have a scroll view over which there is a image view. I'm using this code but my page is not scrolling and only the first image in the string is loaded.Can anyone please help.
Code:
-(void)viewDidLoad
{
count=1;
// imagesName = [[NSMutableArray alloc]initWithObjects :#"election_band_base.jpg", #"ElectionBoxAPPA-Hindi(1).jpg", #"photos.png", #"business.png", #"health.png", nil];
imagesName1 = [NSString stringWithFormat :#"election_band_base.jpg", #"ElectionBoxAPPA-Hindi(1).jpg", #"photos.png", #"business.png", #"health.png", nil];
[imagesName addObject:imagesName1];
items = [[NSMutableArray alloc]init];
// [_imageView1.image setImage=imagesName1];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// _imageView1.image=[UIImage animatedImageWithImages:imagesName duration:0];
_imageView1.image=[UIImage imageNamed:imagesName1 ];
[self loadScrollView];
}
-(void)loadScrollView
{
scrollView.contentSize = CGSizeMake(0, scrollView.frame.size.height);
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [imagesName count]; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
count=1;
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imagesName count], scrollView.frame.size.height);
//scrollView.contentSize = CGSizeMake(900,80);
scrollView.showsHorizontalScrollIndicator =YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = [imagesName count];
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [imagesName count])
return;
// replace the placeholder if necessary
controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:#"iPhone"])
{
controller = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
}
else{
controller = [[MyViewController alloc] initWithNibName:#"MyViewController_ipad" bundle:nil];
}
[controller initWithPageNumber:page];
[controller setArrData:imagesName];
[viewControllers replaceObjectAtIndex:page withObject:controller];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)unloadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [imagesName count]) return;
controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller != [NSNull null]) {
if (nil != controller.view.superview)
[controller.view removeFromSuperview];
[viewControllers replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// NSLog(#"current page %d",page);
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self unloadScrollViewWithPage:page - 2];
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
[self unloadScrollViewWithPage:page + 2];
count=page+1;
// A possible optimization would be to unload the views+controllers which are no longer visible
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViewLoc
{
CGFloat pageWidth = scrollViewLoc.frame.size.width;
CGPoint translation = [scrollViewLoc.panGestureRecognizer translationInView:scrollViewLoc.superview];
int page = floor((scrollViewLoc.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender
{
int page = pageControl.currentPage;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}
So, you want to built the gallery view:
For this implement the following steps:
Add the UIScrollView to the view of your view controller.
In viewDidLoad method: Load the name of the images in the "ImageArray". (I assume all your images have names as "img1.png", "img2.png", "img3.png", ....)
ImageArray=[[NSMutableArray alloc]init];
for (int i=0; i<19; i++) {
NSString *imgtext=[[NSString alloc]initWithFormat:#"img%d",i+1];
[ImageArray addObject:imgtext];
}
In the viewWillAppear method, add the following code:
for (int i = 0; i < ImageArray.count; i++) {
CGRect frame;
frame.origin.x = self.scrollview.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollview.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:#"%#.png",[ImageArray objectAtIndex:i]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setFrame:CGRectMake(0, 0, frame.size.width,frame.size.height )];
[subview addSubview:imageView];
[self.scrollview addSubview:subview];
}
self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * ImageArray.count, self.scrollview.frame.size.height);
self.scrollview.contentOffset=CGPointMake (self.scrollview.frame.size.width, 0);
Hope it helps.
I have made a scroll view in my app in which I have added multiple images with horizontal scrolling. Below is the function which may help you..
- (void)makeFriendsScrollView{
int friendsCount = 10;
float xPos = 10;
for (int i = 0; i < friendsCount; i++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos,25 , 54, 54)];
imgView.image = [UIImage imageNamed:#"user_default.png"];
[imgView.layer setCornerRadius:27.0];
imgView.clipsToBounds = YES;
[scrollViewFriends addSubview:imgView];
xPos = xPos + 64;
}
scrollViewFriends.contentSize = CGSizeMake(friendsCount*65, 90);
[scrollViewFriends.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[scrollViewFriends.layer setBorderWidth:0.5f];
}
i simply did this in view controller check it out...and change according to your requirement..try this in viewWillAppear method
self.arraname=[NSArray arrayWithObjects:#"1.jpg",#"2.jpg",#"3.jpg", nil];
// int friendsCount = 10;
float xPos = 160;
float x1=0;
float y=60;
for (int i = 0; i < [self.arraname count]; i++)
{
x1=xPos+(260*i);
_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x1, y, 54, 54)];
_imgView.image = [UIImage imageNamed:[self.arraname objectAtIndex:i]];
[_imgView.layer setCornerRadius:27.0];
_imgView.clipsToBounds = YES;
[self.scroll addSubview:_imgView];
}
NSLog(#"%f",x1);
self.scroll.contentSize=CGSizeMake(x1+200, 0);
self.scroll.showsHorizontalScrollIndicator = YES;
self.scroll.showsVerticalScrollIndicator=NO;
self.scroll.pagingEnabled = YES;

Received memory warning. in gallery section Xcode

I am making a handbook table cell in my app and i have 7 pages which each are the images and I'm using uiscrollview with page control but when i run the app it says Received memory warning.
and then the app freezes and automatically shuts down, what should i do and this is my code.
#interface HS_HandbookViewController ()
#property (nonatomic, strong) NSArray *pageImages;
#property (nonatomic, strong) NSMutableArray *pageViews;
- (void)loadVisiblePages;
- (void)loadPage:(NSInteger)page;
- (void)purgePage:(NSInteger)page;
#end
#implementation HS_HandbookViewController
#synthesize scrollView = _scrollView;
#synthesize pageControl = _pageControl;
#synthesize pageImages = _pageImages;
#synthesize pageViews = _pageViews;
#pragma mark -
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages we want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what we have to display, then do nothing
return;
}
// Load an individual page, first seeing if we've already loaded it
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what we have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
#pragma mark -
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Student Handbook";
// Set up the image we want to scroll & zoom and add it to the scroll view
self.pageImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"page 1 handbook.png"],
[UIImage imageNamed:#"page 2 handbook.png"],
[UIImage imageNamed:#"page 3 hand book.png"],
[UIImage imageNamed:#"page 4 handbook.png"],
[UIImage imageNamed:#"page 5 handbook.png"],
[UIImage imageNamed:#"page 6 handbook.png"],
[UIImage imageNamed:#"page 7 handbook.png"],
nil];
NSInteger pageCount = self.pageImages.count;
// Set up the page control
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
// Set up the array to hold the views for each page
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set up the content size of the scroll view
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// Load the initial set of pages that are on screen
[self loadVisiblePages];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.scrollView = nil;
self.pageControl = nil;
self.pageImages = nil;
self.pageViews = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages which are now on screen
[self loadVisiblePages];
}
#end
First of all make sure you have not another apps running ( double tap on home button and flick all running apps to close ). May be images are big and there is not enough memory in your device. If you able to run your app after it try to figure out if you have not memory leaks. Swipe few times and follow your memory.
If you use ARC than assign nil to your vars since you are not going to use them.

ios: <Error>: CGAffineTransformInvert: singular matrix

Any reason for this error "CGAffineTransformInvert"
Should I be worried?
I have a .xib with a view, and 4 webViews located outside of the view but within the same xib. Then in the code I add the webViews as subviews to a scroll view inside the view. Would that cause the problem?
Code is below:
//Called first to initialize this class. Also, initializes the nib file and tab bar name.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"More", #"More");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
//Initialize the more tab titles and views
-(void)initViewsandTitles{
MoreTabPages = [NSArray arrayWithObjects:self.aboutWebView,
self.newsUpdateWebView,
self.feedbackWebView,
self.creditsResourceWebView, nil];
titles = [[NSArray alloc] initWithObjects:#"About Locavore",
#"News and Updates",
#"Feedback",
#"Credits and Resources", nil];
}
//Initialize the URLs
-(void)initURLs{
websites = [[NSArray alloc] initWithObjects:#"http://www.getlocavore.com/",
#"http://twitter.com/enjoy_locavore",
#"https://getsatisfaction.com/localdirt/products/localdirt_locavore",
#"http://www.getlocavore.com/about", nil];
}
//Called after the controller's view is loaded into memory.
- (void)viewDidLoad
{
[super viewDidLoad]; //Call the super class init method
[self setupSpinner]; //Start the spinner animatio
[self initViewsandTitles]; //Initialize the views and titles
[self initURLs]; //Initialize the URLs
[self setScrollandPageViewProperties]; //Set the scroll and page view properties
[self setUpPageViews]; //Create the web pages
}
//UIScrollViewDelegate Protocol Reference. Called whn the user scrolls the content within the reciever
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.MoreTabScrollView.frame.size.width;
int page = floor((self.MoreTabScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.MoreTabPageControl.currentPage = page;
self.MoreTabTitle.text = [titles objectAtIndex:page];
}
}
//UIScrollViewDelegate Protocol Reference. Called when the scroll view is about to start scolling content
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
//UIScrollViewDelegate Protocol Reference. Called when the scroll view has ended decelerating the scrolling movement
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(#"DID END SCROLLING");
pageControlBeingUsed = NO;
}
//Called when the page control value changes
- (IBAction)MoreTabChangePage {
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.MoreTabScrollView.frame.size.width * self.MoreTabPageControl.currentPage;
frame.origin.y = 0;
frame.size = self.MoreTabScrollView.frame.size;
[self.MoreTabScrollView scrollRectToVisible:frame animated:YES];
self.MoreTabTitle.text = [titles objectAtIndex:self.MoreTabPageControl.currentPage];
// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed=YES;
}
//Create a frame for each page and add the page to the scroll view
-(void)setUpPageViews{
//Set up all page views for the more tab
for (int i = 0; i < MoreTabPages.count; i++) {
//Get the current table view controller page
UIWebView *webController= [MoreTabPages objectAtIndex:i];
//Request the URL and load the request
NSURL *urll =[NSURL URLWithString:[websites objectAtIndex:i]];
//Run requests in seperate thread
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
NSURLRequest *firstReq = [NSURLRequest requestWithURL:urll];
[webController loadRequest:firstReq];
dispatch_sync(dispatch_get_main_queue(), ^{
//Create a frame for the current table view controller
CGRect frame = webController.frame;
frame.origin.x = self.MoreTabScrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.MoreTabScrollView.frame.size;
webController.frame = frame;
//Add the the current table view controller page to the scroll view
[self.MoreTabScrollView addSubview:webController];
//Release the controller object it is no longer needed
[webController release];
if(i == 3){
[spinner stopAnimating];
}
});
});
}
}
//Set al the properties for the scroll view and page controll
-(void)setScrollandPageViewProperties{
self.MoreTabScrollView.contentSize = CGSizeMake(self.MoreTabScrollView.frame.size.width * MoreTabPages.count,
self.MoreTabScrollView.frame.size.height);
self.MoreTabScrollView.scrollsToTop = NO;
self.MoreTabScrollView.contentOffset = CGPointMake(self.MoreTabScrollView.frame.size.width, 0);
self.MoreTabPageControl.numberOfPages = MoreTabPages.count;
}
-(void)setupSpinner{
spinner.hidesWhenStopped = YES;
[spinner startAnimating];
}
//Called if the application receives a memory warning
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Called when the UIViewController's reference count goes to zero
- (void)dealloc {
[super dealloc];
[MoreTabPageControl release];
[MoreTabScrollView release];
[MoreTabTitle release];
[MoreTabPages release];
[titles release];
[websites release];
[spinner release];
}
#end
try setting the minimum zoom scale for your each webview.
[self.aboutWebView.scrollView setMinimumZoomScale:0.1]
it will throw the same error if the scrollview reaches zero at zero zoom.
This may happen with affine transformations when you're scaling UIScrollView instance to 0 either using setZoomScale:animated: method or zoomScale property, so please check your scroll views.
Make sure your zoomScale, minimumZoomScale and maximumZoomScale to set to at least 0.1.
Related:
Calculating minimumZoomScale of a UIScrollView
UIScrollView not respecting minimumZoomScale after changing the subview

Photo Viewer iPad & iPhone

I've long been trying to make an image viewer, but really I do not what does not work.
Here's a picture like you should get!
This is UIScrollView. The UIImage add to UIScrollView. When user scroll - image must download to UIImageView. I think we need download image in a separate thread. For this I am using - NSOperationqueue and NSInvocationOperation.
This is my code.
In delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
MyThumbnailsBook * myThumbnailsBook = [[MyThumbnailsBook alloc] initWithFrame:CGRectMake(0, 20, 768, 1004)];
[myThumbnailsBook createThumbnails];
[myThumbnailsBook setContentSize:CGSizeMake(768, 1004 * 10)];
[myThumbnailsBook setBackgroundColor:[UIColor blackColor]];
[self.window addSubview:myThumbnailsBook];
[self.window makeKeyAndVisible];
return YES;
}
In class inherit UIScrollView MyThumbnailsBook.m
#define COUNT 100
#import "MyThumbnailsBook.h"
#import "MyImageDownload.h"
#implementation MyThumbnailsBook
#pragma mark -
#pragma mark Initialization & Create Thumbnails
- (id)initWithFrame:(CGRect)frame{
if((self = [super initWithFrame:frame])){
self.delegate = self;
}
return self;
}
- (void)createThumbnails{
float point_x = 20;
float point_y = 20;
for(NSInteger i = 0; i < COUNT; i++){
if(i%3==0 && i != 0){
point_x = 20;
point_y += 220;
}
// Create new image view.
NSURL * url = [[NSURL alloc] initWithString:#"http://storage.casinotv.com/videos/SOYCL/pages/P68.jpg"];
MyImageDownload * imageDownload = [[MyImageDownload alloc] initWithImageURL:url];
[imageDownload setFrame:CGRectMake(point_x, point_y, 200, 200)];
[self addSubview:imageDownload];
[imageDownload release];
[url release];
point_x += 220;
}
}
#pragma mark -
#pragma mark Scroll View Protocol
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
// If scroll view no decelerating.
if(!decelerate){
[self asyncImageDownload];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self asyncImageDownload];
}
#pragma mark -
#pragma mark Download Image
// If scroll stopped - download image to thumbnails.
- (void)asyncImageDownload{
}
#end
In class inherit UIScrollView MyImageDownload.m
#import "MyImageDownload.h"
#implementation MyImageDownload
// This is init method. When class init - we add new operation for download image.
- (id)initWithImageURL:(NSURL*)url{
if((self == [super init])){
[self setBackgroundColor:[UIColor yellowColor]];
NSArray * params = [NSArray arrayWithObjects:url, nil];
queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(asyncDownload:) object:params];
[queue addOperation:operation];
[params release];
}
return self;
}
// Download image to data and after to self (UIImageView).
- (void)asyncDownload:(NSArray *)params{
NSData * data = [NSData dataWithContentsOfURL:[params objectAtIndex:0]]; // Get image data with url.
if(data) [self setImage:[UIImage imageWithData:data]];
[data release];
}
#end
In this example, the images are not loaded and I do not understand why?
I have always used these classes in which the images were loaded from the internet, but I also had problems with memory and I do not know too how to solve it. For example, when to scroll images I get exception - Received memory warning. Level=1 & 2 and after app crash. And I have no idea what to do. I understand what you need as a separate download of images over time and to remove non visible objects but I have found I need the algorithm.
For example - when i go to scrollDidScroll method i get all object when i not see them and remove image:
NSArray *views = [self subviews];
for (UIImageView *v in views) {
if(v.frame.origin.y >= self.contentOffset.y && v.frame.origin.y <= self.contentOffset.y + self.frame.size.height){
// If image of imageview is equal nil - call new operation
if(v.image == nil){
[self requestImageForIndexPath:[NSNumber numberWithInt:v.tag]];
}
else
{
v.image = nil;
}
}
}
self - this is class inherit UIScrollView.
I was confused and ask for help in resolving this issue. TNX all!
The Three20 library includes this functionality in the form of TTPhotoViewController.

Resources