I'm new in iPad development. I'm using AQGridView to build my first ipad app. With the following code, I'm trying to load images from web URLs into each cell of the grid but, when I scroll down and come back up the images are not the same like they should, so they reload to display the good one.
Here's the code I use to load my images.
dispatch_async(dispatch_get_global_queue(0, 0), ^{
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgPath]]];
dispatch_async(dispatch_get_main_queue(), ^{
_imageView.image = img;
[self setNeedsLayout];
});
});
I think the problem comes from here, when calling the dispatch_get_main_queue(). But I dunno how to fix that. If someone has an idea it would for sure help me!
Much thanks in advance!
Regards,
Apple has a demo of a clean solution to exactly this problem in LazyTableImages
Related
I want to add a background image behind my table view.
I have my file staticTableControl.m where I populate the table and I have added
-(void)viewDidLoad{
self.tableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"disk2.jpg"]];
[super viewDidLoad];
}
at the top, but the image doesn't load - please can someone advise me what else I need to do?
Thank you
There have been some other questions about background images for tables, but I couldn't find one specifically for this.
A good solution is to change the tableview background color to transparent and add an imageview behind it
I know this is a common question but it's quite different in my case.
I want an image to be placed when the bar is active and I've done that successfully using these code under didFinishLoadingWithOptions method [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar-active.png"]];
And that's what my app looks like
Now I just want to remove that glossy effect, not the blue image on the bar
Thanks in advance!
After digging a little bit, I found the solution for the same. You simply need to create an UIImage object with empty image.
[yourTabbar setSelectionIndicatorImage:[[UIImage alloc] init]];
Thats it :)
Greetings! I have a problem and Googling brought no results...
I implemented the drawRect method for my NSView (subclass) to draw some shadows and semi-transparent fills. Everything looks great! But now I need to create an NSImage from my NSView (make snapshot) for drag&drop purposes.
It works, but draws in some different manner: darker and not so contrast as should be.
Why? Maybe because of NSGraphicContext different options? Need help and/or advice!
Here is the code for getting NSImage from NSView:
- (NSImage *)makeImageSnapshot {
NSSize imgSize = self.bounds.size;
NSBitmapImageRep * bir = [self bitmapImageRepForCachingDisplayInRect:[self bounds]];
[bir setSize:imgSize];
[self cacheDisplayInRect:[self bounds] toBitmapImageRep:bir];
NSImage* image = [[[NSImage alloc] initWithSize:imgSize] autorelease];
[image addRepresentation:bir];
return image;
}
And here are the images to compare visually:
Normal - drawn by drawRect usual call: http://cl.ly/image/213C1Y1V0v2H
Bad - captured into NSImage: http://cl.ly/image/183q442S2J14
Thought the difference might seem very small, believe me - it is obvious while working with application. I don't undertand why that is happening. And hope someone can help...
Thanks in advance!
I believe the transparency problem is caused by not having other things to blend it against when it's cached on its own.
Try -dataWithPDFInsideRect: or -dataWithEPSInsideRect: and get your image reps from there.
I have
UIView *topPart = [[UIView alloc] initWithFrame:CGRectMake(9, 0, 302, 318)];
topPart.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"pattern.png"]];
[someScroller addSubview:topPart];
[topPart release];
and I can see the pattern fine when I use a 3gs or the iphone simulator, but when I tested on an actual iPhone 4, I see the pattern images but there are black lines separating each pattern so it looks like a grid. I checked the pattern image and there is no black border or anything .
There was someone with a different problem but it may be the same solution
colorWithPatternImage with iPhone 4 Retina Display (image#2x.png)
but they suggested using the -(void)drawRect: method, only problem is i have no idea how to do that.
Thanks
I was experiencing the same issue and found it was due to my png being greyscale (not RGB)
To check if this is the case, just select the image in Xcode, show the Utilities sidebar (the one that appears from the right) and look at the Color Space.
To fix this you can use your favourite image editor, but for photoshop do this:
Open the greyscale image
Select all and copy
Create a new document (but this time make sure the colour space is RGB)
Paste
Hope it helps :)
I figured it out by combining the answers from these 2 posts
https://devforums.apple.com/message/259150#259150
How can I use CGContextDrawTiledImage to tile an image?
UIImage *thePattern= [self GetFineTiles:[UIImage imageNamed:#"Feedbackpattern.png"]];
theView.backgroundColor = [UIColor colorWithPatternImage:thePattern];
-(UIImage*)GetFineTiles:(UIImage*)badImage{
UIGraphicsBeginImageContext(badImage.size);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextDrawTiledImage(imageContext, (CGRect){CGPointZero,badImage.size}, badImage.CGImage);
UIImage *goodPattern = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return goodPattern;
}
Hope this helps others
I tried 2 different methods of creating a background image for a view controller.
I have researched this before and came to the conclusion that for good memory practice you should use this method:
NSString *path = [[NSBundle mainBundle] pathForResource:#"controllerBackground" ofType:#"png" inDirectory:#""];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]]];
When using the other method:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"controllerBackground.png"]]];
This is not as efficient as using the imageWithContentsOfFile:path method.
However when testing both these methods using the performance tools "leak" monitor, I observed a very unusual thing happening.
While using the imageWithContentsOfFile:path method, the problem was that when going back and forth within a navigation controller loading the view controller, the memory would keep increasing within the vicinity of approximately 1mb every time the view controller loads.
This problem is also only observed in the new iOS 4.1 and 4. It does not seem to happen in the iOS 3.0 or 3.2.
Here's a fix that worked for me.
UImage *image = [UIImage imageNamed:#"name.png"];
self.view.layer.contents = (id) image.CGImage;
You have to import QuartzCore, now i don't have 2MB memory increase every time i go that view.
Allan, I faced the same problem and found that colorWithPatternImage and initWithPatternImage is taking large memory than expected. Here is a link which explains the same.
http://www.cocoabyss.com/coding-practice/colorwithpatternimage-abusive-memory-usage/
Better to avoid both methods for background images.