Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set image to UIImage

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

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

My UIImage object is declared in .h file and allocated in init method, I need to set image in viewDidLoad method. Any ideas?

like image 584
Shamsiddin Saidov Avatar asked Jun 07 '12 12:06

Shamsiddin Saidov


People also ask

How do I add an image to UIImage?

Easy way : Drag and drop that image to your file view/project navigator on Xcode. Select the image view - Then attribute inspect - Under image - Select the file name of the image you wish to load. Enjoy the drags and drops whenever you can.

How do I assign an image in Swift?

Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.

How do I get a PHAsset photo?

Use this method if you only need UIImage from PHAsset . extension PHAsset { func image(targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?) -> UIImage { var thumbnail = UIImage() let imageManager = PHCachingImageManager() imageManager.

How do you declare UIImage in Objective C?

For example: UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]]; My UIImage object is declared in .


1 Answers

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.

like image 175
vikingosegundo Avatar answered Oct 02 '22 14:10

vikingosegundo