user2524 user2524 - 5 months ago 12
Objective-C Question

How to display the selected image in same in Xcode?

JSON Url:

{
"status": "200",
"requestType": "productDisplay",
"basePath": "http:\/\/192.168.0.33\/cartwebsite3\/",
"bannerPath": "http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/banner\/",
"productPath": "http:\/\/192.168.0.33\/cartwebsite3\/cdn-images\/prd\/",
"response": {
"data": [{
"product_id": "3",
"seller_id": "1",
"product_active": "on",
"product_name": "Micromax Unite 3",
"product_alias": "(Blue, 8 GB)",
"product_sku": "21334",
"product_manufacturer_country": "India",
"product_manufacturer": "india",
"product_min_add": "1",
"product_max_add": "100",
"short_description": "Slim, compact and user-friendly, the Micromax Unite 3 is a good combination of powerful performance and nifty features for a great smartphone experience.",
"long_description": "Slim, compact and user-friendly, the Micromax Unite 3 is a good combination of powerful performance and nifty features for a great smartphone experience.",
"product_quantity": "0",
"quantity": "100",
"status": "In Stock",
"price_id": "4",
"mrp_price": "5900.0000",
"selling_price": "5099.0000",
"offer_percentage": "",
"product_weight": "130g",
"offer_selling_price": "2000.0000",
"offer_selling_percentage": "",
"offer_selling_start_date": "1461621600",
"offer_selling_end_date": "1469484000",
"shipping_cost": "0.0000",
"product_images": [{
"image_id": "18",
"product_id": "3",
"image_small": "",
"image_medium": "",
"image_large": "",
"original_res": "micromax-unite-3-951452579749.jpeg",
"image_type": "jpeg",
"added_date": "1452579749",
"added_user": "1",
"sort": "0"
}, {
"image_id": "19",
"product_id": "3",
"image_small": "",
"image_medium": "",
"image_large": "",
"original_res": "micromax-unite-3-241452579749.jpeg",
"image_type": "jpeg",
"added_date": "1452579749",
"added_user": "1",
"sort": "0"
}, {
"image_id": "20",
"product_id": "3",
"image_small": "",
"image_medium": "",
"image_large": "",
"original_res": "micromax-unite-3-301452579749.jpeg",
"image_type": "jpeg",
"added_date": "1452579749",
"added_user": "1",
"sort": "0"
}],
"basic_info": [{
"basic_id": "550",
"product_id": "3",
"option_name": "Features",
"option_description": "Android v5 OS",
"have_connected": "0"
}, {
"basic_id": "551",
"product_id": "3",
"option_name": "Features",
"option_description": "8 MP Primary Camera",
"have_connected": "0"
}, {
"basic_id": "552",
"product_id": "3",
"option_name": "Features",
"option_description": "2MP Secondary Camera",
"have_connected": "0"
}, {
"basic_id": "553",
"product_id": "3",
"option_name": "Features",
"option_description": "Dual Sim (GSM WCDMA)",
"have_connected": "0"
}],
"extra_info": [{
"type": "textfield",
"main_header": "Micromax Unite 3(Blue, 8 GB)",
"field_option": [{
"inside_single_title": "Specifications",
"basic_opt1": [{
"option_name_extra": "In the Box",
"option_desc_extra": "Handset, USB Cable, Charger, User Guide, Warranty Card, Battery, Hands-free"
}, {
"option_name_extra": "Sensors",
"option_desc_extra": "Light Sensor, Gravity Sensor, Proximity Sensor"
}, {
"option_name_extra": "User Memory\t",
"option_desc_extra": "4.9 GB for Apps and Phone Storage"
}, {
"option_name_extra": "Processor",
"option_desc_extra": "1.3 GHz MTK 6582M, Quad Core"
}]
}]
}],
"category_list": [{
"category_id": "46",
"category_name": "ELECTRONICS",
"category_desc": "",
"category_image": "",
"category_thumbnail": "",
"category_image_desc": "",
"category_meta_title": "",
"category_meta_desc": "",
"category_meta_keyword": "",
"category_show_hide": "0",
"category_vanity": "electronics"
}, {
"category_id": "48",
"category_name": "Mobiles",
"category_desc": "",
"category_image": "",
"category_thumbnail": "",
"category_image_desc": "",
"category_meta_title": "",
"category_meta_desc": "",
"category_meta_keyword": "",
"category_show_hide": "0",
"category_vanity": "mobiles"
}, {
"category_id": "54",
"category_name": "Micromax",
"category_desc": "",
"category_image": "",
"category_thumbnail": "",
"category_image_desc": "",
"category_meta_title": "",
"category_meta_desc": "",
"category_meta_keyword": "",
"category_show_hide": "0",
"category_vanity": "micromax"
}]
}]
},
"request": {
"postData": [],
"getData": {
"type": "productDisplay",
"result": "json",
"product_id": "3"
}
}
}


Code:

-(void)navigateToNextPage :(UITapGestureRecognizer *)gest{

UIImageView *theTappedImageView = (UIImageView *)gest.view;
NSInteger index = theTappedImageView.tag;

ViewController *orderView=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
[self.navigationController pushViewController:orderView animated:YES];
self.imgName.image = [imgid objectAtIndex:index];

// orderView.stringcell=[product_id objectAtIndex:index];




}


How to display the selected image in same image in Xcode. image is clicked already but image is not displayed in output.

Answer

ViewController.h

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_collectionView;
}

@end

ViewController.m

@interface ViewController ()
{
    NSArray *imagesArr;
    UIImageView *mainImageView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    imagesArr = [NSArray arrayWithObjects:@"sample1.jpeg", @"sample2.jpeg", nil];


    mainImageView = [[UIImageView alloc] init];
    mainImageView.frame = CGRectMake(10, 10, 300, 300);
    mainImageView.image = [UIImage imageNamed:[imagesArr objectAtIndex:0]];
    [self.view addSubview:mainImageView];


    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 60, [[UIScreen mainScreen] bounds].size.width, 60) collectionViewLayout:layout];
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:_collectionView];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return imagesArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];


    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.frame = CGRectMake(0, 5, 50, 50);
    imgView.image = [UIImage imageNamed:[imagesArr objectAtIndex:indexPath.row]];
    [cell addSubview:imgView];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(50, 50);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",[imagesArr objectAtIndex:indexPath.row]);
    mainImageView.image = [UIImage imageNamed:[imagesArr objectAtIndex:indexPath.row]];
}

This is just a sample code. Replace/Assign "imagesArr" to your images array.

Hope this helps.

Comments