I am loading an image in a UIImageView which I then add to a UIScrollView.
The image is a local image and is about 5000 pixels in height.
The problem is that when I add the UIImageView to the UIScrollView the thread is blocked.
It is obvious because when I do this I can not scroll the UIScrollView till the image is displayed.
Here's the example.
UIScrollView *myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
myscrollview.contentSize = CGSizeMake(7680, 1004);
myscrollview.pagingEnabled = TRUE;
[self.view addSubview:myscrollview];
NSString* str = [[NSBundle mainBundle] pathForResource:#"APPS.jpg" ofType:nil inDirectory:#""];
NSData *imageData = [NSData dataWithContentsOfFile:str];
UIImageView *singleImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:imageData]];
//the line below is the blocking line
[scrollView addSubview:singleImageView];
It is the last line in the script that blocks the scroller. When I leave it out everything works perfect, except for the fact the image is not showing of course.
I seem to recall that using multithreading does not work on UIView operations so I guess that's out of the question ass well.
Thanks for your kind help.
If you're providing these large images, you should maybe check out CATiledLayer; there's a video of a good presentation on how to use this from WWDC 2010.
If these aren't your images, and you can't downsample them or break them into tiles, you can draw the image on a background thread. You may not draw to the screen graphics context on any main thread but the main thread, but that doesn't prevent you from drawing to a non-screen graphics context. On your background thread you can
create a drawing context with CGBitmapContextCreate
draw your image on it just as you would draw onto the screen in drawRect:
when you're done loading and drawing the image invoke your view's drawRect: method on the main thread using performSelectorOnMainThread:withObject:waitUntilDone:
In your view's drawRect: method, once you've fully drawn your image on the in-memory context, copy it to the screen using CGBitmapContextCreateImage and CGContextDrawImage.
This isn't trivial, you'll need to start your background thread at the right time, synchronize access to your images, etc. The CATiledLayer approach is almost certainly the better one if you can find a way to manipulate the images to make that work.
Why you want to load such huge image to memory at one time? split it into many small images and load/free it dynamically.
Try allocating the UIImageView without a UIImage, and add it as a sub-view to the UIScrollView.
Load the UIImage in a separate thread, and get the method running on the other thread to set the image property of the UIImageView when the image is loaded into memory. Also you'll probably encounter some memory problems as an image of this size loaded into a UIImage will probably be 30MB+
UIScrollView *myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1004)];
myscrollview.contentSize = CGSizeMake(7680, 1004);
myscrollview.pagingEnabled = TRUE;
[self.view addSubview:myscrollview];
NSString* str = [[NSBundle mainBundle] pathForResource:#"APPS.jpg" ofType:nil inDirectory:#""];
UIImageView *singleImageView = [[UIImageView alloc] init];
[scrollView addSubview:singleImageView];
//Then fire off a method on another thread to load the UIImage and set the image
//property of the UIImageView.
Just keep an eye on memory and beware of using convenience constructors with UIImage (or any object that could end up being huge)
Also where are you currently running this code?
Related
Is it possible to draw several UIViews with custom drawing on single CALayer so that they dont have backing store each?
UPDATE:
I have several uiviews of the same size which have same superview. Right now each of them has custom drawing. And because of big size they create 600-800 mb backing stores on iPad 3. so i want to compose their output on one view and have several times less memory consumed.
Every view has it's own Layer and you can't change that.
You could enable shouldRasterize to flatten a view hierarchy, which might help in some cases, but that needs gpu memory.
another way could be to create an image context and merge the drawings into the image and set that as the layer content.
in one of the wwdc session videos of last year which was about drawing showed a drawing app where many strokes were transfered into a image to speed up drawing.
Since the views will share the same backing store, I assume you want them to share the same image that results from the layer's custom drawing, right? I believe this can be done with something similar to:
// create your custom layer
MyCustomLayer* layer = [[MyCustomLayer alloc] init];
// create the custom views
UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, layer.frame.size.width, layer.frame.size.height)];
UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake( 100, 100, layer.frame.size.width, layer.frame.size.height)];
// have the layer render itself into an image context
UIGraphicsBeginImageContext( layer.frame.size );
CGContextRef context = UIGraphicsGetCurrentContext();
[layer drawInContext:context];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// set the backing stores (a.k.a the 'contents' property) of the view layers to the resulting image
view1.layer.contents = (id)image.CGImage;
view2.layer.contents = (id)image.CGImage;
// assuming we're in a view controller, all those views to the hierarchy
[self.view addSubview:view1];
[self.view addSubview:view2];
I have subclassed an UILabel and i use drawrect method to draw the string on the screen. This is my code:
- (void)drawRect:(CGRect)rect
{
CGRect aRect = self.frame;
NSMutableArray *Text=[globaltextarray Text];
[[NSString stringWithCString:[[Text objectAtIndex:labelindexpath.row] cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding] drawInRect:aRect
withFont:[UIFont fontWithName:#"Times New Roman" size:15.0] lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
[super drawRect:rect];
}
It is working fine but it is slow for my purpose. I need to draw the string without using [Label setNeedDisplay]; because setNeedsDisplay needs to be called from the main thread. The rendering work needs to be done in a background queue. I would be grateful if you could provide me with a small example. Any help appreciated.
Thanks.
You can create a new CGContext to draw the string into CGImage/UIImage using CGBitmapContextCreateImage on separate thread, then draw the bitmap(generated image) into view on the main thread.
EDIT:
Check this post for example.
I have a 400 pattern images at 400x300 bundled within my app. I would like to make some kind of factory method to take a portion of that image and load them into UIImageViews. I've had some success with using content mode and clipping to bounds, but when I load a ton of these into a view it can take upwards of 5 seconds for the view to load. Here is an example of my current method.
UIImageView *tinyImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed#"400x300testImage.png"];
[tinyImageView setFrame:CGRectMake(0, 0, 10, 200)];
[tinyImageView setContentMode:UIViewContentModeTopLeft];
[tinyImageView setClipsToBounds:YES];
[self.tinyImagesView addSubview:tinyImageView];
I've been reading the ImageIO class files and I think my answer is in there but I'm having a hard time putting together workable code. In another stackoverflow question I came across this code
CFDictionaryRef options = (__bridge CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform,
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
(id)[NSNumber numberWithFloat:200.0f], (id)kCGImageSourceThumbnailMaxPixelSize,
nil];
CGImageRef imgRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options);
UIImage *scaled = [UIImage imageWithCGImage:imgRef];
CGImageRelease(imgRef);
CFRelease(imageSource);
return scaled;
This has a similar load time to loading the full images and clipping.
Is it possible to read in only a 10x200 strip of an image file and load that into a UIImageView that is as fast as creating that 10x200 png and loading that using imageNamed?
I'm pretty sure what you really want is a CATiledLayer, where you can point it at the set of images and have it automatically pull up what it needs.
You can just add a CATiledLayer to any UIView.
Is it possible to set an image over an other image,
respectively to overlay an image above an UIImage.
I give some code:
Headerfile:
#property (nonatomic,assign)UIImageView* imageView;
Implementationfile
UIImage *overlayImage = [UIImage imageNamed:#"lamp.png"];
The "imageView.image" has got an image and above that I will have this lamp, but later on it has to be possible to move it.
I searched some methods, but all methods gave me warnings.
Could you help me how to manage it? And is Core Animation an alternative to handle that?
Just add it as a subview. UIImageView is a UIView like any other.
UIImage *overlayImage = [UIImage imageNamed:#"lamp.png"];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:overlayImage];
[self.imageView addSubview:overlayImageView];
You should use another UIImageView. A UIImage doesn't refer to an actual view in the interface, it's used as a reference to an image to be reused in code.
UIImageView* lampImageView= [[UIImageView alloc] initWithImage:overlayImage];
This way you can move it around anywhere over the first image view.
I have an NSBox set up in my main view that accepts drag and drop. We store the URL into str. We then load the image and add it to the content view of NSBox.
imageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, 390, 150)];
NSURL *imageURL = [NSURL URLWithString:str];
NSImage *image = [[NSImage alloc] initByReferencingURL:imageURL];
[imageView setImage:image];
[self setContentView:imageView];
However, this doesn't do anything. The image is not displayed in the nsbox.
Another oddity. At first I was trying to add the NSImageView via interface builder, but the only image container they had was IKImageView... Is this odd? Isn't NSImageView more ubiquitous? I mainly develop on iOS, so I have the version from the ios developer site.
Any thoughts?
Edit: I should also add, when I was using IKImageView in IB, the image would show up.
Edit2: I've also tried just taking the initWithFrame out and replacing it with just init, no go.
The problem was that I was using initByReferencingURL to get the NSImage. This doesn't work for local files, so instead I used initByReferencingFile and everything worked out well!