Right now, I've just some code which fetches the picture from the URL directly.
ViewController.h
(...)
#property (retain, nonatomic) IBOutlet UIImageView *imageView;
(...)
ViewController.m:
#import Header.h
(...)
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *imageURL = [NSURL URLWithString:#"http://www.visitingdc.com/images/eiffel-tower-picture.jpg"];
NSData *myImageData = [NSData dataWithContentsOfURL:imageURL];
imageView.image = [UIImage imageWithData:myImageData];
}
My goal is to view a picture in a imageView which link is stored in a database. The JSON will send the data from the database as a string:
[
{
"image":"http://www.visitingdc.com/images/eiffel-tower-picture.jpg"
}
]
which will be stored in the variable *image in Header.
Header.h
#interface Header : NSObject {
NSString *image;
}
#property (nonatomic, copy) NSString *image;
- (id)initWithDictionary:(NSDictionary *)dictionary;
+ (NSArray *)findAllRemote;
#end
What do I have to write in the NSURL-code in ViewController.m so it fetches the data from the variable instead of a URL-String?
You can store json response in NSSTring, like
self.image = ;//where you have parsed JSON data
and then use -
NSURL *imageURL = [NSURL URLWithString:self.image];
Related
I have created a program to retrieve JSON file and it achieved it
NSString *FilePath = [[NSBundle mainBundle]pathForResource:#"Message" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:FilePath];
NSError *error;
if(error){
NSLog(#"Error and CAn't retrive data: %#", error.localizedDescription);
}else{
NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(#"Your Json Dictionary values are %#", jsonDict);
for(NSDictionary *valuesDictionary in jsonDict){
ShopCollectionObject *shopObject = [[ShopCollectionObject alloc]initWithID:[[valuesDictionary objectForKey:#"message_id"]integerValue] Name:[valuesDictionary objectForKey:#"product"] TimeAsPrice:[[valuesDictionary objectForKey:#"message_time"]integerValue] Avathar:[valuesDictionary objectForKey:#"item_image"] user:[valuesDictionary objectForKey:#"user_image"] Name_User:[valuesDictionary objectForKey:#"user_name"] LocationOfUser:[valuesDictionary objectForKey:#"locate_user"]];
But My app crashes here with the above error
[self.objectForArray addObject:shopObject];
}
}
Updated my shop collection code below
Shopcollection object.h
#import <Foundation/Foundation.h>
#interface ShopCollectionObject : NSObject
-(instancetype) initWithID: (int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int) GivenTimeAsPrice Avathar:(NSString *) PhotoOfAvathar user:(NSString *)UserAvathar Name_User: (NSString *) UserNames LocationOfUser:(NSString *) USerLocationGiven;
#property (nonatomic) int msgID;
#property(nonatomic, strong)NSString* Name;
#property (nonatomic) int TimeAsPrice;
#property (nonatomic,strong) NSString* Avathar;
#property (nonatomic,strong) NSString* user;
#property (nonatomic,strong) NSString* Name_User;
#property(nonatomic,strong) NSString* LocationOfUser;
#end
Shopcollectionobject.m
#import "ShopCollectionObject.h"
#implementation ShopCollectionObject
-(instancetype)initWithID:(int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int)GivenTimeAsPrice Avathar:(NSString *)PhotoOfAvathar user:(NSString *)UserAvathar Name_User:(NSString *)UserNames LocationOfUser:(NSString *)USerLocationGiven{
self = [super init];
if(self){
self.msgID = msgID;
self.Name = Profile_name;
self.TimeAsPrice = GivenTimeAsPrice;
self.Avathar = PhotoOfAvathar;
self.user = UserAvathar;
self.Name_User = UserNames;
self.LocationOfUser = USerLocationGiven;
}
return self;
}
#end
You likely aren't initializing your objectForArray. So when you try to call addObject, it's calling it on a null object.
ShopCollectionObject.h
#import <Foundation/Foundation.h>
#interface ShopCollectionObject : NSObject
#property (nonatomic) int message_id;
#property (strong, nonatomic) NSString *Name;
#property (nonatomic) int TimeAsPrice;
#property (strong, nonatomic) NSString *Avathar;//user,Name_User,LocationOfUser,message_id
#property (strong, nonatomic) NSString *user;
#property (strong, nonatomic) NSString *Name_User;
#property (strong, nonatomic) NSString *LocationOfUser;
-(instancetype) initWithID: (int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int) GivenTimeAsPrice Avathar:(NSString *) PhotoOfAvathar user:(NSString *)UserAvathar Name_User: (NSString *) UserNames LocationOfUser:(NSString *) USerLocationGiven;
#property (nonatomic) int msgID;
#end
ShopCollectionObject.m
#import "ShopCollectionObject.h"
#implementation ShopCollectionObject
-(instancetype)initWithID:(int)msgID Name:(NSString *)Profile_name TimeAsPrice:(int)GivenTimeAsPrice Avathar:(NSString *)PhotoOfAvathar user:(NSString *)UserAvathar Name_User:(NSString *)UserNames LocationOfUser:(NSString *)USerLocationGiven{
self = [super init];
if(self){
self.msgID = msgID;
self.Name = Profile_name;
self.TimeAsPrice = GivenTimeAsPrice;
self.Avathar = PhotoOfAvathar;
self.user = UserAvathar;
self.Name_User = UserNames;
self.LocationOfUser = USerLocationGiven;
}
return self;
}
#end
ViewController.m
#import "ViewController.h"
#import "ShopCollectionObject.h"
#interface ViewController ()
{
NSMutableArray *objectForArray;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
objectForArray = [[NSMutableArray alloc]init];
NSString *FilePath = [[NSBundle mainBundle]pathForResource:#"Message" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:FilePath];
NSError *error;
if(error){
NSLog(#"Error and CAn't retrive data: %#", error.localizedDescription);
}else{
NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for(NSDictionary *valuesDictionary in jsonDict){
ShopCollectionObject *shopObject = [[ShopCollectionObject alloc]initWithID:[[valuesDictionary objectForKey:#"message_id"]intValue] Name:[valuesDictionary objectForKey:#"product"] TimeAsPrice:[[valuesDictionary objectForKey:#"message_time"]intValue] Avathar:[valuesDictionary objectForKey:#"item_image"] user:[valuesDictionary objectForKey:#"user_image"] Name_User:[valuesDictionary objectForKey:#"user_name"] LocationOfUser:[valuesDictionary objectForKey:#"locate_user"]];
[objectForArray addObject:shopObject];
}
NSLog(#"%#",objectForArray);
ShopCollectionObject *data = objectForArray[0];
NSLog(#"%#",data.Name);
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
pls check this code
I would like to get an image from an URL and put it into a NSArray, but doesn't work in the below codes, please help:
#interface DataViewController ()
#property (readonly, strong, nonatomic) NSArray *picData;
#end
#implementation DataViewController
- (void)viewDidLoad {
NSString *imageUrlStringAA=#"http://aaa.net/AA.png";
NSString *imageUrlStringBB=#"http://aaa.net/BB.png";
NSURL *urlAA=[NSURL URLWithString:imageUrlStringAA];
NSURL *urlBB=[NSURL URLWithString:imageUrlStringBB];
NSData *dataAA=[[NSData alloc] initWithContentsOfURL:urlAA];
NSData *dataBB=[[NSData alloc] initWithContentsOfURL:urlBB];
UIImage *imageAA=[UIImage imageWithData:dataAA];
UIImage *imageBB=[UIImage imageWithData:dataBB];
_picData = [NSArray arrayWithObjects:imageAA, imageBB, nil];
Any ideas is appreciated, thank you.
First thing for download anything from http protocol then Add following diction in info.plist
And now try this code:
#interface DataViewController ()
#property (readonly, strong, nonatomic) NSArray *picData;
#end
#implementation DataViewController
- (void)viewDidLoad {
NSString *imageUrlStringAA=#"http://www.hdwallpapers.in/download/antelope_canyon-360x640.jpg";
NSString *imageUrlStringBB=#"http://www.hdwallpapers.in/download/vegeta_dragon_ball_super-360x640.jpg";
NSURL *urlAA=[NSURL URLWithString:imageUrlStringAA];
NSURL *urlBB=[NSURL URLWithString:imageUrlStringBB];
NSData *dataAA=[[NSData alloc] initWithContentsOfURL:urlAA];
NSData *dataBB=[[NSData alloc] initWithContentsOfURL:urlBB];
UIImage *imageAA=[UIImage imageWithData:dataAA];
UIImage *imageBB=[UIImage imageWithData:dataBB];
_picData = [NSArray arrayWithObjects:imageAA, imageBB, nil];
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:#"Image URL"]]];
dispatch_sync(dispatch_get_main_queue(), ^{
if(image!= nil){
[[cell myimage] setImage:image];
}else{
UIImage * image = [UIImage imageNamed:#"PLACEHOLDER"];
[[cell myimage] setImage:image];
}
[cell setNeedsLayout];
});
});
Try these...
I am having the view controller class like this
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) NSDictionary *dictionary;
#end
ViewController.m
#import "ViewController.h"
#import "GoogleMaps.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//HOW TO ACCESS THE PROPERTY VALUE HERE
self.dictionary = #{};
/ DUMP ALL FOUND ITEMS
for(DummyContainer* geoItem in geoItems) {
NSDictionary *item = #{
#"latitude":geoItem.latitude,
#"longtitude":geoItem.longtitude
};
self.dictionary[geoItem.geoPoint.name] = item;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The expected format is
var container = {
'location':{
'latitude':1233,
'longtitude':124
}
}
This can be accessible via
let obj = container['location'];
for latitude access like this
obj.latitude;
Question1: How to create a class property as dictionary and access inside the class?
Question2: How to create JSON structure and access the values?
I am new to iOS please help me thanks in advance.
For creating non extendable/immutable Dictionary Object
#property (strong, nonatomic) NSDictionary *myClassDictionary;
For creating extendable/mutable Dictionary Object
#property (strong, nonatomic) NSMutableDictionary *myClassMutableDictionary;
Insert all of your values inside a Dictionary like this
You exampleData
'location':{
'latitude':1233,
'longtitude':124
}
NSDictionary *dict = #{#"lattitude":#"1233" , #"longitude":#"124"};
self.myClassDictionary = #{#"location":dict};//Convert this dictionary into JSON.
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: self.myClassDictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString jsonString;
if (! jsonData) {
NSLog(#"Got an error: %#", error);
} else {
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
In my app i have created a custom camera viewcontroller and i would like to save the image in the documents folder under a specific path with the image's location info.
This is how i save the image now:
NSData* originalImageJpegRepresentation = UIImageJPEGRepresentation(self.cameraSnapshotImage, 90);
[originalImageJpegRepresentation writeToFile:[self.targetDirectory stringByAppendingPathComponent:filename] atomically:YES];
But the image doesn't contain the location info in its exif data after i read it.
I do have the geo location at the time i wish to save it in a CLLocation* object.
How can i add its coordinate to the saved image?
Thanks in advance
Create a custom subclass of NSObject and then use NSCoding to manually store and retrieve the image data and location data.
#interface MyImageData : NSObject <NSCoding>
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) CLLocation *location;
#end
#implementation MyImageData
...
#pragma mark NSCoding
- (void) encodeWithCoder:(NSCoder *)encoder {
NSData *imageData = UIImageJPEGRepresentation(_image, 90);
[encoder encodeObject:originalImageJpegRepresentation forKey:#"image"];
[encoder encodeObject:_location forKey:#"location"];
}
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self) {
NSData *imageData = [decoder decodeObjectForKey:#"image"];
_image = = [[UIImage alloc] initWithData:imageData];
_location = [decoder decodeObjectForKey:#"location"];
}
return self;
}
Then you can save an instance of this class to file and it will include both data items.
MyImageData *myImageData = ...
[myImageData writeToFile:filePath atomically:YES];
You will need to use the ALAssetsLibrary to save the gps data using the writeImageToSavedPhotosAlbum method. Read up on it here
I'm making a Card game and trying to call UIImages from an object's instance variable to update a UIImageView
I have a Deck object, which has an NSArray instance variable of Card objects.
Each Card object has a few instance variables, one of which is an UIImage that I'm trying to display in a UIImageView....and this is where I'm having a problem
The storyboard isn't displaying the UIImageView and i'm not getting any compile errors
The UIImageView that I'm trying to update is cardDisplay (ViewController.h)
Here's some snippets from my code
ViewController.h
#import "Deck.h"
#import "Card.h"
#interface ViewController : UIViewController
{
UIImageView *cardDisplay;
}
#property (nonatomic, retain) IBOutlet UIImageView *cardDisplay;
#end
ViewController.m
#import "ViewController.h"
#import "Deck.h"
#import "Card.h"
#implementation ViewController
#synthesize cardDisplay;
- (void)viewDidLoad
{
[super viewDidLoad];
Deck *deck = [[Deck alloc]init];
NSLog(#"%#", deck);
for (id cards in deck.cards) {
NSLog(#"%#", cards);
}
self.cardDisplay = [[UIImageView alloc] initWithImage:
[[deck.cards objectAtIndex:0 ] cardImage]];
}
#end
Card.h
#interface Card : NSObject
{
NSString *valueAsString, *suitAsString;
NSInteger faceValue, countValue;
Suit suit;
UIImage *cardImage;
}
#property (nonatomic, retain) NSString *valueAsString;
#property (nonatomic, retain) NSString *suitAsString;
#property (nonatomic) NSInteger faceValue;
#property (nonatomic) NSInteger countValue;
#property (nonatomic) Suit suit;
#property (nonatomic) UIImage *cardImage;
- (id) initWithFaceValue:(NSInteger)aFaceValue countValue:(NSInteger)aCountValue
suit:(Suit)aSuit cardImage:(UIImage*)aCardImage;
#end
Deck.h
#import "Card.h"
#interface Deck : NSObject
{
NSMutableArray *cards;
}
#property(nonatomic, retain)NSMutableArray *cards;
#end
Deck.m
#import "Deck.h"
#import "Card.h"
#implementation Deck
#synthesize cards;
- (id) init
{
if(self = [super init])
{
cards = [[NSMutableArray alloc] init];
NSInteger aCount, picNum = 0;
for(int suit = 0; suit < 4; suit++)
{
for(int face = 1; face < 14; face++, picNum++)
{
if (face > 1 && face < 7)
aCount = 1;
else if (face > 6 && face < 10)
aCount = 0;
else
aCount = -1;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *imagePath = [path stringByAppendingPathComponent:
[NSString stringWithFormat:#"/cards/card_%d.png",picNum]];
UIImage *output = [UIImage imageNamed:imagePath];
Card *card = [[Card alloc] initWithFaceValue:(NSInteger)face
countValue:(NSInteger)aCount
suit:(Suit)suit
cardImage:(UIImage *)output];
[cards addObject:card];
}
}
}
return self;
}
#end
This line:
self.cardDisplay = [[UIImageView alloc] initWithImage:
[[deck.cards objectAtIndex:0 ] cardImage]];
should be:
self.cardDisplay.image = [[deck.cards objectAtIndex:0 ] cardImage];
You need to set the image on the image view you created in IB, not create a new one. Doing it this way doesn't keep you from doing what you want with the timer later.