Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 08-17-2010, 01:49 PM   #1 (permalink)
Registered Member
 
screwtape's Avatar
 
Join Date: Jul 2010
Location: Nottingham
Posts: 54
screwtape is on a distinguished road
Send a message via Skype™ to screwtape
Angry Inherited NSCoding adherence worthless in UIImageView

It seems to be an interesting bit of dodgy coding by Apple here.

My understanding of protocol adherence in general, and for NSCoding in particular, it is essential that if the superclass adhears to it, then the subclass must do too.

However, Apple seem to have forgotten that with UIImageView, as although according to the letter of protocol adhearence, the UIImageView does implement encodeWithCoder, it doesn't work if there is an attached UIImage object, which correct me if I'm wrong, is the whole point of UIImageView!

What makes this worse, is that the failure to properly implement NSCoding in UIImageView makes the implementation for UIView utterly useless unless you can be certain there are no UIImageViews in the subtree.
screwtape is offline   Reply With Quote
Old 08-17-2010, 02:08 PM   #2 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

I don't think UIImageView can do any better than it does. Since it does not know where the UIImage comes from, there's no way for it to know what to code. It would be wasteful for it to code the actual bytes of the image, since you probably already have it saved to a file somewhere.

It seems like a simple solution is to subclass UIImageView. Then your code would know where the image came from, and how to read it back.
JasonR is offline   Reply With Quote
Old 08-17-2010, 09:29 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 342
flamingliquid is on a distinguished road
Default

The point of the NSCoder for UIViews is to save state, such as the frame, visibility, opacity, etc.
__________________
17 year old nerd.
Send me a PM. Maybe I can help.
flamingliquid is offline   Reply With Quote
Old 08-18-2010, 02:57 AM   #4 (permalink)
Registered Member
 
screwtape's Avatar
 
Join Date: Jul 2010
Location: Nottingham
Posts: 54
screwtape is on a distinguished road
Send a message via Skype™ to screwtape
Default

Quote:
Originally Posted by JasonR View Post
I don't think UIImageView can do any better than it does. Since it does not know where the UIImage comes from, there's no way for it to know what to code. It would be wasteful for it to code the actual bytes of the image, since you probably already have it saved to a file somewhere.

It seems like a simple solution is to subclass UIImageView. Then your code would know where the image came from, and how to read it back.
You make a valid point there, but if that is the case, the UIImageView should have a encodeWithCoder method that specifically excludes saving the image, and it should be documented as such.

Simply allowing it to call the encodeWithCoder of the UIImage (which of course doesn't exist) causes the encoding to fail, and means that you can't encode UIImageView, or any UIView contaning it, which is my argument.
screwtape is offline   Reply With Quote
Old 08-18-2010, 11:15 AM   #5 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

When you say it "fails" no one knows what you mean. I assumed it failed to encode correctly. I think you are saying it causes a crash due to an exception. I haven't had a chance to try it myself.
JasonR is offline   Reply With Quote
Old 08-19-2010, 03:29 AM   #6 (permalink)
Registered Member
 
screwtape's Avatar
 
Join Date: Jul 2010
Location: Nottingham
Posts: 54
screwtape is on a distinguished road
Send a message via Skype™ to screwtape
Default

Quote:
Originally Posted by JasonR View Post
When you say it "fails" no one knows what you mean. I assumed it failed to encode correctly. I think you are saying it causes a crash due to an exception. I haven't had a chance to try it myself.
Sorry, you're right - I'm not being clear.

It crashes because UIImage doesn't implement encodeWithCoder. This means I can't save my view tree, which I'd hoped I could, as I want to be able to reload it quickly, rather than reconstructing it from scratch, which is quite slow.

I can't currently think of any other way storing the view tree.
screwtape is offline   Reply With Quote
Old 08-19-2010, 09:33 AM   #7 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

The only solution I can see for you is to subclass UIImageView. It's a pain, because it sounds like you might have to write the encode and decode yourself entirely.

You might also be able to add a category to UIImage to implement encodeWithCode. I've never tried it, but it might work.
JasonR is offline   Reply With Quote
Old 08-19-2010, 11:41 AM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

iPhone Development: UIImage and NSCoding

Google is your friend.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
nscoding, uiimage, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 378
15 members and 363 guests
AragornSG, ChrisYates, coolman, davejas69, givensur, hussain1982, iAppDeveloper, jbro, Kryckter, locombiano89, Mah6447, Meoz, stanny, Tomsky, WeaselPig
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,646
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, locombiano89
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:45 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0