开发者

Dynamically loading images in UIImageView from database in objective c

开发者 https://www.devze.com 2023-01-19 06:30 出处:网络
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

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 excep开发者_开发技巧t 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消