Cocoa-Touch: use button or UIImage for clickable image? - objective-c

I'm building a iOS app which involves lots of photos displayed as rectangular areas on the screen. You can click on that area and see a bigger version of the photo. Kind of like those wallpaper apps, where you first see a grid layout of small wallpapers and then you can go in to see a bigger version.
So, I'm wonder what's the pros/cons in terms of memory usage and scalability between setting them up as UIButton (w/ background) and UIImageView?

I'd use an UIButton. It's easier to use as a user because it's made for touching them. Since UIImageView directly inherits from UIView it doesn't really know targets and selectors. So you'd have to implement this using touchesBegan:withEvent:. UIImageView is more efficient at drawing pictures though. UIButton already knows that so you'd only have to set the picture.

Related

How to change from NSView subviews to CaLayers in a MacOs App?

I have a music macOS Application that displays a piano roll with note events in a midi file. I am representing them as bars in a view, using a main NSView and a lot (typically 5000/10000) of subviews.This performs quite well, the only problem is on intensive tasks, like when I zoom the view, or I change the proportions from the GUI, I believe all the views receive a setNeedsDisplay message and resize accordingly. I was thinking,
Could I switch my implementation to CALayers instead of subviews ? the big NSView works in a NSScrollview. Perhaps I could try to implement some sort of lazy updating where only the visible notes get updated.
Thanks for any help.

Stacking UIViews with blend modes in iOS

I have two different UIImageViews. I'd like to make the top UIImageView blend in using the Screen blend mode with the bottom UIImageView.
I know of the property of CALayer: compositingFilter and I know that it doesn't work in iOS. I've searched a lot for solutions, and I've found how one should subclass UIView and override drawRect.
I've tried to set the context in drawRect to the screen blend mode, although it still draws every one of the images normally. Perhaps I am doing something wrong, or the approach should be different. Maybe I need OpenGL or CALayer to achieve this. Could someone assist?
Unfortunately there is no way to do a non-composite blend between UIViews on iOS. UIKit doesn't provide the functionality, and you've already noted, CALayer can't do it either.
In general, implementing -drawRect in a UIView won't help you. You're drawing into an empty bitmap -- it doesn't contain the bits of the views behind it, since those might change at any time (any view or layer might be animated). CA fundamentally assumes that layers' contents should be independent of each other.
You could try, in your -drawRect:
create an image context
capture the views under your view using -[CALayer renderInContext:] for each
create an image from the image context
draw that image into your view
set the blend mode and draw on top of that
But that will be slow and fragile, and won't work if you animate any of the views. I wouldn't recommend it.
If you really need to do this, you're going to have to switch your whole scene to render with OpenGL, where you've got more freedom.

How to draw in code an imageview

I have a problem. I want for each record in my core data database to draw an UIImage view on screen. But the problem is that I want to make a sort of grid. On the link below you see what I want to achieve.
picture
So my question is, how do I draw an image on screen in code. And place those images in a sort of a grid. using a collection view is no option, because the app should be running on all IOS devices.
While you could implement a custom UIView and implement the drawRect: method and draw UIImages there, I suggest just using multiple UIImageViews as subviews on your "main" view. Your view might be embedded in a UIScrollView, or you could use a UITableView with custom UITableViewCells. Whichever is easier is probably related to how you can interact with the view.
Building that one huge image view is something that I'd definitely try to avoid - it costs many many (probably unnecessary) memory, and it might be slow as well. Definitely not very flexible to handle, and a pain to update dynamically.
A quick cheat for something like this is to use a Table View and then in each cell to place another TableView but rotated at 90 degrees.
You can then use this second TableView to display the pictures etc...
This will give you a table that scrolls up and down and then each cell can scroll left to right.
I'd suggest subclassing UITableViewCell and setting it up as a UITableViewDelegate and UITableViewDatasource.
You will also have to remember to rotate the content of these "sub"tables by 90 degrees also so that they are the right way up.
This sounds like a lot of work but if you push the management of the sub Tables into the cells then it actually becomes quite easy.

What is the best way to create a composite scrollable view on iOS

I need to create a scrollable composite view on iOS. That is to say, the view will contain at least one image, possibly a button, and some text (that we may wish to format with bold fonts, etc). The amount of data, and particularly the amount of text, is variable, from maybe 4 lines to maybe 100. The data is "variable" to a degree, and in particular the image and text do not come joined at the hip.
This all needs to fit in a "pane" of about 280h x 115w pixels in a portrait-only layout.
A single UITextView doesn't provide the facilities to display an image or format the text.
A UIWebView provides the ability to display the image and formatted text, but the button is a problem (not even sure if it's doable).
A UIScrollView would easily allow the image and button, and then a UIWebView could be embedded in the scroll view for the text, but then scrolling becomes a problem -- I'd like the entire view to scroll as one, without having to resize the web view to contain it's content, and without the confusion of a scrollable within a scrollable (the doc warns of "unexpected behavior").
(I'm guessing your thoughts at this point are that I want too much.)
So, any suggestions? What's the best way to get close to what I need here?
In iOS5 the UIWebView has a scrollView property, which is a normal UIScrollView. You should be able to add a UIButton as a subview of the scrollView to achieve what you want, although positioning it correctly may be a challenge. Prior to iOS5 you could cycle through the subviews of the UIWebView to find the UIScrollView with isKindOfClass...
I suggest testing with a UIWebView inside your UIScrollView. I don't see any interference in the iOS 5.0 simulator. I don't know if there are problems in iOS 4.0.
If you find that there is interference, you can prevent it by setting the web view's userInteractionEnabled property to NO, either in the nib or in code. This will prevent the web view from receiving any touches, so the user also won't be able to pinch-zoom it, follow links in it, or copy text from it.
In the web view's delegate, implement webViewDidFinishLoad: to set the web view's size and the scroll view's contentSize. For example:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = self.webView.frame;
frame.size = [self.webView sizeThatFits:CGSizeMake(frame.size.width, HUGE_VALF)];
self.webView.frame = frame;
self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(frame), CGRectGetMaxY(frame));
}
When I did a similar thing, I had a dozen of views which I added to the UIScrollView and then calculated the frames of all the views. Granted, it was an extremely tedious work, given that some views could get hidden under various conditions. The layout code was actually pretty simple, laying out views from top to bottom, but ugly. The upshot is that it works like a charm, fast and reliably. You can even trivially wrap it in an animation block.

How to control the Appearance of UIPickerView

Can anyone point me in the right direction here.
Im trying to use a UIPickerView that is displaying images.
The images are too big for the default pickerview setup so I was wondering how I could control how big the rows in the UIPickerView are and how wide the translucent selection bar is to accomodate for their custom size.
Can anyone provide some guidelines as to how big I should make the row images or where a good tutorial / book chapter is on this?
Thanks!
Why not use the UIImagePickerController instead of trying to customize a UIPickerView? It is designed to enable you to allow the user to pick from their library of images if you use the sourceType of UIImagePickerControllerSourceTypePhotoLibrary.
There is sample code on how to use this controller at http://zcentric.com/2008/08/28/using-a-uiimagepickercontroller/
If the images are not from the user's library, then I would suggest you scale the images down to fit the constraints of the UIPickerView, or use a tabelView with the images laid out in it for the user to select.