why my application getting crash in my iPod? - iphone

I am adding UIImageView on UIScrollView but my app getting crash when array count is high.
Here is my code
scr=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 320, 320)];
scr.tag=1000;
[scr setDelegate:self];
[self.view addSubview:scr];
dispatch_queue_t queeee =dispatch_queue_create("raj", 0);
int countt=200;
for(int i=0;i<countt;i++)
{
CGRect rectt=CGRectMake((i%3*90)+30,(i/3*90)+40,80,70);
UIImageView *asyImagee = [[UIImageView alloc]initWithFrame:rectt];
dispatch_async(queeee, ^{
NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Default" ofType:#"png"]];
__block UIImage *imgg=[UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^
{
asyImagee.image=imgg;
imgg=nil;
});
});
asyImagee.tag=i;
[scr addSubview:asyImagee];
asyImagee=nil;
}
[scr setContentSize:CGSizeMake(0, countt/3*110)];
Working fine with int countt=100;
but crash with 200;

Related

Downloading Images Asynchronously

I am using the Given Code to download images from its respective url's, but due to some problem it's not working and i am unable to diagnose the problem. Please help me out this Problem.
Thanks in Advance.
-(void)createScrollViewControls
{
[self removeAllControlsFromScrollView];
totalNumberOfImages=[arrImages count];
__block int x=0;
__block int y=0;
__block int scroll=0;
__block UIImage *tempimag=nil;
if(totalNumberOfImages>0) //If product images are there
{
// imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);
for(int i=0;i<totalNumberOfImages;i++){
NSLog(#"Value=%#",arrImages);
UIActivityIndicatorView *activity= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(77+scroll, 38, 30, 30)];
[activity setBackgroundColor:[UIColor clearColor]];
[activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[imageScroll addSubview:activity];
[activity startAnimating];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
NSURL *imageURL=[NSURL URLWithString:[arrImages objectAtIndex:i]];
NSData *tempData=[NSData dataWithContentsOfURL:imageURL];
UIImage *tImage=[UIImage imageWithData:tempData];
if (tImage==nil)
{
tImage=[UIImage imageNamed:#"NoImage.png"];
}
dispatch_async(dispatch_get_main_queue(), ^{
tempimag= tImage;
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y, IMAGEWIDTH, IMAGEHEIGHT)];
//if url is there but image not getting downloaded then show a static image
if (!tempimag)
{
imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);
NSLog(#"no underlying data");
imageView.image = [UIImage imageNamed:kNoImageAvailable];
[imageScroll addSubview:imageView];
// totalNumberOfImages = 0;
}
else{
[activity stopAnimating];
imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);
imageView.image=tempimag;
[imageScroll addSubview:imageView];
x=x+imageScroll.frame.size.width;
}
});
});
/*
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(x, y, IMAGEWIDTH, IMAGEHEIGHT)];
if (!tempimag)//if url is there but image not getting downloaded then show a static image
{
imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);
NSLog(#"no underlying data");
imageView.image = [UIImage imageNamed:kNoImageAvailable];
[imageScroll addSubview:imageView];
totalNumberOfImages = 0;
break;
}
else{
imageScroll.contentSize=CGSizeMake(imageScroll.frame.size.width*totalNumberOfImages, IMAGEHEIGHT);
imageView.image=tempimag;
[imageScroll addSubview:imageView];
x=x+imageScroll.frame.size.width;
}
} */
scroll+=184;
}
}
This below lines of code:
if (tImage==nil)
{
tImage=[UIImage imageNamed:#"NoImage.png"];
}
will always make your program control to go to:
if (!tempimag)
{
imageScroll.contentSize=CGSizeMake(imageView.frame.size.width, IMAGEHEIGHT);
NSLog(#"no underlying data");
imageView.image = [UIImage imageNamed:kNoImageAvailable];
[imageScroll addSubview:imageView];
// totalNumberOfImages = 0;
}
Please remove the first part of the code mentioned and try it out.

How to fix the emplacement of the picture in xcode

I'm playing with the tabbar, navigationBar and SegmentedBar in my application.
I have to show an picture from a HTTP like this :
- (void)viewDidLoad {
[super viewDidLoad];
// build the URL, perform the request and show the picture
NSURL *url = [NSURL URLWithString: #"http://api.clementhallet.be/15avril.png"];
//UIImage
[[UIImageView alloc] initWithImage:image];
image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
image.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
}
It's running but the picture isn't where I want [exactly][1].
Thanks to help me!
This should be the right order:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:imageView];
You've got to set the frame of your imageview for positioning. If not in interface builder done, do it like this:
image.frame = CGRectMake(x, y, width, height);

The latest iPhone 4 has image display issue

The older 3GS, or iPod 4 are able to show the final tempimage, but on the iPhone 4, I don't see the tempview showing up on screen.
I stepped through it, getting imageData, tempimage allocated, tempview allocated, but the final tempview is not showing up.
-(void) display:(NSData*) imageData
{
tempimage= [[[UIImage alloc] initWithData:imageData] autorelease];
tempview=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
if (tempimage) {
[tempview setImage:tempimage];
[self.view addSubview:tempview];
}
try like this..
-(void) display:(NSData*) imageData
{
if(imageData != nil)
{
tempimage= [[UIImage alloc] initWithData:imageData];
}
else
{
tempimage= [UIImage imageNamed:#"something.png"];
}
tempview=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
if (tempimage) {
[tempview setImage:tempimage];
[self.view addSubview:tempview];
}
if(imageData != nil)
{
[tempimage release];
}
[tempview release];
}

UIButton doesn't interact in UIScrollView

I have a windowed (e.g. not a full-screen) UIScrollView (inside a UIView) which scrolls through groups of UIImageViews with UIButtons on them (the idea being you click the button to do something with the displayed image). The UIButton does take any touch events - how can I fix it?
I've read this, this, this, this and this - but I either don't understand the answer or its implications, or it's not really relevant.
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
Sentence *testSentence = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:i];
//NSLog(#"testSentence: %#", testSentence);
//NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
//Your going to need to optimise this by creating another thumbnail image to use here.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imageName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[testSentence image]];
//NSLog(#"imageName: %#", imageName);
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageName];
//NSLog(#"image: %#", image);
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//NSLog(#"imageView: %#", imageView);
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setBackgroundColor:[UIColor blackColor]];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
imageView.frame = rect;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTitle:#"Play" forState:UIControlStateNormal];
aButton.frame = CGRectMake(10, 10, 70, 70);
[aButton setUserInteractionEnabled:YES];
[aButton setEnabled:YES];
[aButton setAlpha:1];
UIView *buttonWrapperUIView = [[UIView alloc] init];
[aButton addTarget:self action:#selector(clickPlay:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:aButton];
[imageView bringSubviewToFront:aButton];
[scrollView1 addSubview:imageView];
NSLog(#"aButton %#", aButton);
[image release];
[imageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
try
[imageView setUserInteractionEnabled:YES]

UIPageControl is not Displayed

I use the following to display scrollview and pagecontrol
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 179)];
pageControl=[[UIPageControl alloc] initWithFrame:CGRectMake(0, 179, 320, 20)];
scrollView.delegate = self;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
int index=[[self._parameters objectForKey:#"index"] intValue];
NSString *imageNamePrefix=[[activeAppIdList objectAtIndex:index] objectForKey:#"AppID"];
int noOfScreenShot=[[[activeAppIdList objectAtIndex:index] objectForKey:#"NoOfScreenShot"] intValue];
CGFloat cx = 0;
for (int j=1;j<=noOfScreenShot ; j++) {
UIImage *image =[[UIImage alloc] init];
image= [self loadImage:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
if (image == nil) {
NSString *urlString = [[NSString alloc]initWithFormat:#"http://localhost/MoreApp/images/%#_%d.jpg",imageNamePrefix,j];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
UIImage *temp=[[UIImage alloc]initWithData:imageData] ;
[self saveImage:temp withName:[[NSString alloc]initWithFormat:#"%#_%d.jpg",imageNamePrefix,j]];
image = temp;
[temp release];
[urlString release];
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[image release];
imageView.frame = CGRectMake(cx,0,scrollView.frame.size.width,scrollView.frame.size.height);
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
NSLog(#"No Of pages..%d",noOfScreenShot);
pageControl.numberOfPages = noOfScreenShot;
pageControl.currentPage = 0;
scrollView.contentSize = CGSizeMake(cx,scrollView.frame.size.height);
NSLog(#"Width:%f",scrollView.frame.size.width);
[ refToSelf.instructionView addSubview:scrollView];
[ refToSelf.instructionView addSubview:pageControl];
[scrollView release];
[pageControl release];
scrollview is ok but pagecontroll didn't showed...
can help??
If the page control's and container's background color is the same (by default white) page control won't be visible.
For anyone who finds this, another silly mistake you can make is to not set the numberOfPages attribute on the UIPageControl, which will also cause it to not display.
My mistake was equally annoying - developing with storyboard in 3.5 inch view, and built on 4 inch device on simulator so it was offscreen. Silly!