How to convert data to image in iOS
NSData *imageData = UIImagePNG开发者_运维技巧Representation(your_image_here);
After that i transferred it to another iPhone using bluetooth. Now i need to convert the data back to image. Can any one tell me how to do it?
I assume this is to et back the data which you encoded in the previous question.
For converting UIImage
to NSData
,
NSData* pictureData = UIImagePNGRepresentation(image);
To get it back,
UIImage *image = [[UIImage alloc]initWithData:pictureData];
Remember to add [image release]
at the end.
or you can use
UIImage *image = [[UIImage alloc]init];
[image imageWithData:pictureData];
In swift-3 : Data is converted back to image by :--
func compressImage() -> UIImage {
let imageData = UIImageJPEGRepresentation(UIImage(named:"background.jpg")!, 0.2)
let image = UIImage(data: imageData!)
return image!
}
Just call imageWithData:
or on instantiation, you can call initWithData:
[UIImage imageWithData:(NSData *)data];
精彩评论