App getting crash in background image loading iphone - iphone

I have scroll view with 60 UIImageView's. Images displaying in these imageviews are from url and url's i get from the webservice. When user scrolls to bottom I call the webservice and get new 60 urls. After getting the url i am utilizing same UIImageView's to display images. Following is my code for displaying images.
for (UIView *viewSel in [scrlView subviews]) {
NSString *strImgUrl = [arrImgURL valueForKey:#"url"];
if ([viewSel isKindOfClass:[UIImageView class]]) {
UIImageView *imgView = (UIImageView *)viewSel;
[imgView setImage:[UIImage imageNamed:#"loading432x520.png"]];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:[NSString stringWithFormat:#"%#",strImgUrl]];
[arr addObject:imgView];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
[arr release];
}
}
- (void) loadImageInBackground:(NSMutableArray *)arr {
NSLog(#"loadImage");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[arr objectAtIndex:0]]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
if (img != nil) {
[arr addObject:img];
}
else{
[arr addObject:[UIImage imageNamed:#"no-image432x520.png"]];
}
[img release];
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
[pool release];
}
- (void) assignImageToImageView:(NSMutableArray *)arr{
NSLog(#"assignImage");
UIImageView *imgView = [arr objectAtIndex:1];
imgView.image = [arr objectAtIndex:2];
}
The code works perfect for first time. But when i get new urls it is working some time or getting crash. I don't know why it is getting crash. I want to stop that crash. If you are not getting to my question then let me know. Please help me for this. Thanks in advance. Valid answer will be appreciated.

Thank you all to give your help full comments to my question. I got the error. I am adding CALayer to uiimageview when allocating memory to uiimageview. I remove that CALayer code and it work perfectly. Nikita's comment help me to find my error.

Related

Error in Load image in NSThread?

I have a url which contain image address, i want to load that image via NSThread but i am facing problem. I am doing thing like this.
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 55, 57)];
[NSThread detachNewThreadSelector:#selector(showImage) toTarget:self withObject:nil];
[self.view addSubview:imageView];
- (void) showImage {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:temp.strUrl];
UIImage *chart = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
[url release];
imageView.image = chart;
[pool release];
}
Please help me on that.
the problem is here.
[url release];
you are not supposed to release the url object . as you haven't alocated it, may be that is what you are facing problem with.

upload image from url in scroll view

I have array of image in scroll view. I want to upload bigger image from url when i tap on image.
for example i tap on image"page-001.jpg" then check image data and then upload bigger image on image view and get back option so that go back to previous image view.
try this
NSMutableArray *arr = [[NSArray alloc] initWithObjects:imageURL,imageView.tag, nil];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
- (void) loadImageInBackground:(NSArray *)urlAndTagReference
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Retrieve the remote image. Retrieve the imgURL from the passed in array
NSURL *imgUrl=[[NSURL alloc] initWithString:[urlAndTagReference objectAtIndex:0]];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
UIImage *img = [UIImage imageWithData:imgData];
[imgUrl release];
// Create an array with the URL and imageView tag to
// reference the correct imageView in background thread.
NSMutableArray *arr = [[NSMutableArray alloc ] initWithObjects:img,[urlAndTagReference objectAtIndex:1], nil ];
// Image retrieved, call main thread method to update image, passing it the downloaded UIImage
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
[arr release];
[pool release];
}
- (void) assignImageToImageView:(NSMutableArray *)imgAndTagReference
{
UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:20];
imageView.image = [imgAndTagReference objectAtIndex:0];
}
You can use SDWebImage. Just add the files to your project and use
[UIImageview setImageWithURL:(NSURL*)url];
This library also manage cache, and works very well in an UITableViewCell.

UIImageView setImage Leaks?

Have been searching for a solution to this problem for endless hours now. The problem is really simple. I have a UIImageView in the nib, i'm loading a image from internet and sets the UIIImageView with the image. I'm releasing the viewController. Dealloc is actually getting called(!) and loads the viewController and the image again. This can be done by entering background mode really easy. The Instrument Leaks doesn't report any leaks, BUT it shows in allocation that it keeps the image in the memory and it keeps growing.
Basic example:
-(id)init {
if((self = [super init])) {
id path = [NSString stringWithFormat:#"http://www.aSite.com/largeImage.jpg"];
NSData* urlData = [[NSData alloc] initWithContentsOfURL:url];
UIImage* img = [[UIImage alloc] initWithData:urlData];
[background setImage:img];
[urlData release];
[img release];
}
return self;
}
-(void)dealloc {
[background release];
[super dealloc];
}
Some people say that UIImageView actually leaks, or actually CGImage. Some people say that Instrument aren't showing correctly. I'm getting Memory Warning after 10-15 times of doing this with a 2.5mb large image. Results are from real device and latest iOS (or atleast 4-5 weeks ago). As UIImageView is used by so many people i thought it would be easy to find the problem or get a fix from apple?
Source of CGImage Leak:
(iphone) UIImageView setImage: leaks?
EDIT: I was abit tierd when i wrote the example. Example is correct now. I have also tried with autoreleased object and same "leak" is still there. Please answer the question if you gonna write an answer.
release the url after setting the image
[background setImage:[UIImage imageWithData:urlData]];
[urlData release];
[img release];
In the following code you are making some mistakes.
[urlData release];
[background setImage:[UIImage imageWithData:urlData]];
you should use
if((self = [super init])) {
id path = [NSString stringWithFormat:#"http://www.aSite.com/largeImage.jpg"];
NSData* urlData = [[NSData alloc] initWithContentsOfURL:url];
UIImage* img = [[UIImage alloc] initWithData:urlData];
[background setImage:img];
[urlData release];
[img release];
}
Can't you replace
[background setImage:[UIImage imageWithData:urlData]];
with
[background setImage:img];
UPDATE
I think this should also help
if((self = [super init])) {
id path = [NSString stringWithFormat:#"http://www.aSite.com/largeImage.jpg"];
NSData* urlData = [[NSData alloc] initWithContentsOfURL:url];
[background setImage:[UIImage imageWithData:urlData]];
[urlData release];
}
Have you tried:
-(id)init {
if((self = [super init]))
{
[background setImage:
[UIImage imageWithData:
[NSData dataWithUrl:
[NSUrl urlWithString:
#"http://www.aSite.com/largeImage.jpg" ]]]
];
}
return self;
}
-(void)dealloc {
[super dealloc];
}
clean and with no memory leaks!

I'm getting a EXC_BAD_ACCESS when using the viewWillAppear:(BOOL)animated method

So I'm accessing some data (settings) from a .plist as my view appears. When I use the viewDidAppear:(BOOL)animated method to access that data everything works just fine and dandy. But when I access the data using the viewWillAppear:(BOOL)animated method to access my data everything stops and I get an EXC_BAD_ACCESS error.
Can someone please help me out with this?
- (void)viewDidAppear:(BOOL)animated {
NSLog(#"View Will Appear method");
NSString *filePath = [self settingsFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
if ([[array objectAtIndex:0] intValue] == 0) {
UIImage *imageLow = [UIImage imageNamed:#"image1.png"];
[object1 setImage:imageLow];
[imageLow release];
unitRatio = 1.8;
}
else if ([[array objectAtIndex:0] intValue] == 1) {
UIImage *imageHigh = [UIImage imageNamed:#"image2.png"];
[object1 setImage:imageHigh];
[imageHigh release];
unitRatio = 0.9;
}
[array release];
}
else {
UIImage *imageLow = [UIImage imageNamed:#"image1.png"];
[object1 setImage:imageLow];
[imageLow release];
unitRatio = 1.8;
}
}
The [imageLow release] and [imageHigh release] calls are unnecessary and will cause a crash. The objects returned by imageNamed: are pre-autoreleased, so you don't need to release them yourself.
Also, you must have a call to super in your viewWillAppear method:
[super viewWillAppear:animated]
That's not what is causing the crash, but the docs say it's required.

AFOpenFlow touchevent Image

does anybody know a solution that i could implement at this code,
that the pictures could be touched and an event will be started?
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
[imageName release];
[aktuellesImage release];
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
I hope someone could help me and
sorry for my bad english :)
in AFOpenFlowView.m methods touchesBegan, touchMoves and touchEnded are already overriden, so just create a new delegate method for your usage and adapt these methods.