How to set image to UIImage

后端 未结 12 896
小蘑菇
小蘑菇 2020-12-14 06:20

How can I set image to UIImage object. For example:

UIImage *img = [[UIImage alloc] init];
[img setImage:[UIImage imageNamed:@\"anyImageName\"]]         


        
相关标签:
12条回答
  • 2020-12-14 06:50

    just change this line

    [img setImage:[UIImage imageNamed:@"anyImageName"]];
    

    with following line

    img = [UIImage imageNamed:@"anyImageName"];
    
    0 讨论(0)
  • 2020-12-14 06:51

    Just Follow This

    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
    [imgview setImage:[UIImage imageNamed:@"YourImageName"]];
    [imgview setContentMode:UIViewContentModeScaleAspectFit];
    [self.view addSubview:imgview];
    
    0 讨论(0)
  • 2020-12-14 06:53

    may be:

    UIImage *img = [[UIImage alloc] init];
    

    and when you want to change the image:

    img = [UIImage imageNamed:@"nameOfPng.png"];
    

    but the object wasn't in the same place in the memory, but if you use the pointer, the same pointer will be point to the last image loaded.

    0 讨论(0)
  • 2020-12-14 06:55
    UIImage *img = [UIImage imageNamed:@"anyImageName"];
    

    imageNamed:
    Returns the image object associated with the specified filename.

        + (UIImage *)imageNamed:(NSString *)name
    

    Parameters
    name
    The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.
    Return Value
    The image object for the specified file, or nil if the method could not find the specified image.

    Discussion
    This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

    0 讨论(0)
  • 2020-12-14 06:55

    Create a UIImageView and add UIImage to it:

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image Name"]] ;
    

    Then add it to your view:

    [self.view addSubView: imageView];
    
    0 讨论(0)
  • 2020-12-14 06:58

    First declare UIImageView and give it frame

    UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake( 10.0f, 15.0f, 40.0f,40.0f )];
            [imageView setBackgroundColor: [UIColor clearColor]];
            [imageView setImage:[UIImage imageNamed:@"comments_profile_image.png"]];
            [self.view addSubview: imageView];
    
    0 讨论(0)
提交回复
热议问题