I am trying to load UIImage
object from NSData
, and the sample code was to NSImage
, I guess they should be the same. But just now loading the image, I am wondering what's the best to troubleshoot the UIImage
loading NSData
issue.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
I didn't try
UIImageJPEGRepresentation()
before, butUIImagePNGRepresentation
works fine for me, and conversion betweenNSData
andUIImage
is dead simple:For safe execution of code, use if-let block with Data, as function UIImagePNGRepresentation returns, optional value.
Generic image operations (like png & jpg both):
By using extension:
theData should be a
NSData
object which already contains the data. You need to do the file loading/downloading to theNSData
object before it is used. You can inspect it by usingNSLog
on theData and see if it contains the valid data.UIImage has an -
initWithData:
method. From the docs: "The data in the data parameter must be formatted to match the file format of one of the system’s supported image types."Try this to convert an image to NSdata: