views:

82

answers:

1

I have saved my images in database with type BLOB. Now I am extracting these images from database using sql stmt and saving it in a NSData object. see the following code snippet

NSData *img = [[NSData alloc] initWithBytes:sqlite3_column_blob(loadInfostmt, 4)  length: sqlite3_column_bytes(loadInfostmt, 4)];

self.birdImage = [UIImage imageWithData:img];  

After getting image I tried setting it at image attribute of UIImageView to load it to image view. I used following techniques but nothing seems to work.

1.

self.imgPikr = [[UIImageView alloc] initWithImage:brd.birdImage];  
[self.imgPikr setImage:brd.birdImage];   

2.

[self.imgPikr = [[UIImageView alloc] init];    
self.imgpikr.image = brd.birdImage;

3.

   [self.imgPikr = [[UIImageView alloc] init];    
   [self.imgpikr setImage:brd.birdImage];

Here imgPikr is a UIImageView object and birdImage is object of UIImage class. every other data is displayed in view correctly except the image. I dont have any error or warning or runtime exception but still cant load image. Please tell me the solution. I have been working on it since a week. but couldnt find anything working. Any answer or experimentation is welcomed.

thanx in advance

A: 

Option 1 is the best option, since -[UIImageView initWithImage:] will automatically resize the imageView to be the same size as the passed image (and it is also unnecessary to setImage: if you use this initializer).

However, as @Robot K points out in the comments, you still need to add self.imgPikr as a subview of a view that is visible on the screen.

Additionally, if your imgPikr property is declared as (retain), then you have a memory leak.

Dave DeLong
yes it is (retain). how should I improve it? Should I use Copy instead. If yes plsease specify reasons as well.
Nitesh
@Nitesh `autorelease` the new imageview before setting the property. Reason? -> read the memory management guidelines: http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
Dave DeLong
thanx dave I will update and upgrade my code. Can you please suggest anything regarding problem i mentioned above. your help is appreciated
Nitesh