How can I store a UIImage in an NSDictionary?

前端 未结 6 1168
无人及你
无人及你 2021-02-01 20:30

How can I store a UIImage in an NSDictionary?

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 20:31

    OK I had this problem recently and the above solutions never worked for me. So if someone else finds their way here to solve the same problem, here is how it's done

    NSDictionary *imageDictionary = @{
     kImageKey:  UIImageJPEGRepresentation(/*INSERT YOUR UIImage HERE*/,0.1)
                                     };
    

    // the 0.1 represents the compression quality 0.0 being lowest to 1.0 being the highest quality

    then to get image back from dictionary use

    [UIImage imageWithData:[imageDictionary objectForKey: kImageKey]];
    

提交回复
热议问题