base64编码图像并将其添加到UIImage

时间:2015-05-12 06:49:13

标签: ios xcode uiimage nsdata

我正在创建一个客户需要的验证码图像。 我正在使用两个键值获取图像的JSON。

key: CaptchaText, value: uEU3VNWh0lndY8UyL+jWsQ==

key: CaptchaImage, value: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABGAJYDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD1OiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAotrWlJffYW1OyF5uC/ZzOvmZPQbc5zV6vE/ivYSaR4vstatg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fPI+nJ/CuTLKM69b2NLaTSXqzTB2VaXstYW1b7nnvi+6l8bfEiHSrV8wRSC1jKnI4OZH/AJ/gor22O1hstMW1t02Qww+Wi+igYAryv4N6EWkvNdmQ4H+jwE9zwWP8h+detSgtC4AySpAr6vPq0I1qeCpfDSSXz6v+utzvo+9J1H1PE/gz/wAjRe/9eZ/9DWvb6+bfBvio+D9WnvDZfajJEYSnm+XjkHOcH0r1Lwn8Tf8AhJ9ej0z+xzbb0ZvNFxvxgZ6bR/Ou7iTK8XVxM8VCF4JLW66LXS9zKjNRVn3PQKKKK+OOoKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD/9k= 

我想将此键CaptchaImage附加到UIImage。 以下是我正在使用的代码。

        NSString *tempStr = [dictionary valueForKey:@"CaptchaImage"];
        NSLog(@"temp Ste :- %@",tempStr);//1
        NSData* dataNewOne = [tempStr dataUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"Data to be :- %@",dataNewOne);//2
        temp = [UIImage imageWithData:dataNewOne];
        NSLog(@"image width:%f",temp.size.width);//3
        NSLog(@"img :- %@",temp);//4

代码工作正常,但是给我空值。

  1. NSLog给我这样的CaptchaImage

    温度步骤: - / 9J / 4AAQSkZJRgABAQEAYABgAAD / 2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL / 2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL / wAARCABGAJYDASIAAhEBAxEB / 8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL / 8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2J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Kdf6vyPn7dTfooorQ4wooooAKKKKACiiigAooooA8p + M + q6B9gt9JvLOS71dx5lqYW2tBk4yTg5BI + 7jnHbg15tq / grxlpHhi1u9RgnfSoj5ptTOWFvk9WQH5c56jpnnBrr / iz4d1q08XQeKbC1kurZFR2KRlxC0fPzgdF4znp1qtq3xnvtf0WfRrTw + qXV7H9n3rOZclvlO1NoJJzxyecdamN + V23uU90nseheAfEOka74JaPSrNbAWcZiktFORGcE5B6kHk5PJOc1zfgHSLLVdWma9iEqwIHWNvuk57jv9KtfCjwlqHh3w3qd5qcT289 + oKwOMMiKrYLDsTuPB6YqT4YD / iZX57eSv86566UsXC + un6M8zFRUqlJPa7 / QoePNLtLDxDClpCkKTxKzIgwoOSOB26Vc8WeDjpcMN / paf6NDGBMCfmDA / fOeucj6Y9Oi / EF / AJGOx / 64R / 6Ea2vHmrXllZGzW0ElldQbGmwRsfPHPToOlcU4Q9nUbXV2 / T5HrcPyr0s1m8JFOdtnondXa6bnG6dfa 2fDWoWdjAv9ngmS4lx90EAFQScenA5ro / AQN / hE9abHB3gH / tnXLaVJq95pk + i6ZbvJHPKrzMi8 + gBPRRx39K9U0XQU0rw4NMLAvIjec47swwce3b6CqoQlUvJXfu2179kfV4mr9Uy32FalGlOUk + SLvZJ3u7N / I5L4Xf67U / 8Adj / 9mqj8SP8AkaLf / R1T / wBDeqekapfeB9WuobqyLl12tGW2ZwThlODkdar + J5tVvtQh1HVLU2wnBWCIjBVFPTHXqep659MVKqJ0Y0 + qev3s9qnhJvOfrl1ySjo7rXRbLfpf0PaT1NJSnqaSvXPzUKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD / 9K =

  2. 第二个NSLog给我这样的NSData

    数据是: - < 2f396a2f 34414151 536b5a4a 52674142 41514541 59414267 4141442f 32774244 41416747 42676347 42516748 4277634a 4351674b 4442514e 4441734c 44426b53 45773855 48526f66 48683061 48427767 4a43346e 49434973 49787763 4b446370 4c444178 4e445130 48796335 50546779 5043347a 4e444c2f 32774244 41516b4a 4351774c 4442674e 44526779 49527768 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4979 4d6a4c2f 77414152 43414247 414a5944 41534941 41684542 41784542 2f385141 48774141 41515542 41514542 41514541 41414141 41414141 41414543 41775146 42676349 43516f4c 2f385141 74524141 41674544 41774945 41775546 42415141 41414639 41514944 41415152 42524968 4d554547 45314668 42794a78 46444b42 6b614549 49304b78 77525653 3066416b 4d324a79 67676b4b 46686359 47526f6c 4a69636f 4b536f30 4e545933 4f446b36 51305246 526b6449 53557054 56465657 5631685a 576d4e6b 5a575a6e 61476c71 63335231 646e6434 65587144 68495747 6834694一个69704b54 6c4a5757 6c35695a 6d714b6a 704b576d 70366970 71724b7a 744c5732 74376935 75734c44 784d5847 78386a4a 79744c54 314e5857 31396a5a 32754869 342b546c 3575666f 36657278 38765030 39666233 2b506e36 2f385141 48774541 41774542 41514542 41514542 41514141 41414141 41414543 41775146 42676349 43516f4c 2f385141 74524541 41674543 42415144 42416346 42415141 41514a33 41414543 41784545 42534578 42684a42 55516468 63524d69 4d6f4549 46454b52 6f624842 43534d7a 55764156 596e4c52 4368596b 4e4f456c 38526359 47526f6d 4a796770 4b6a5532 4e7a6735 4f6b4e45 52555a48 53456c4b 55315256 566c6459 5756706a 5a47566d 5a326870 616e4e30 64585a33 65486c36 676f4f45 68596148 69496d4b 6b704f55 6c5a6158 6d4a6d61 6f714f6b 7061616e 714b6d71 73724f30 74626133 754c6d36 77735045 78636248 794d6e4b 30745055 31646258 324e6e61 3475506b 3565626e 364f6e71 38765030 39666233 2b506e36 2f396f41 44414d42 41414952 41784541 50774431 4f696969 67416f6f 6f6f414b 4b4b4b41 43696969 67416f6f 6f6f414b 4b4b4b41 43696969 67416f6f 6f6f414b 4b4b4b41 43696969 67416f6f 6f6f4151 6b4b4353 51414f53 54327249 6a38572b 47357055 69693851 6155386a 73465646 76597957 4a364144 504a7279 6a343661 74664455 744f3064 5a586973 48683835 774d685a 48334566 4e363763 4134392f 70555869 6a345436 446f7667 69625772 54574c69 53654b4a 4a413872 6f597073 34345541 5a476338 636e3861 56394f5a 37446131 35657037 7052586d 6e775931 4b396e38 44542f62 476b6547 306e5a49 47626b37 416f4f30 5a37416b 2f774171 36533338 65364c50 5a543354 4e4e4173 52414353 714e3068 49504367 45353655 716b3455 33615438 7a4a3159 52747a4f 31372f68 75645053 45685153 53414279 53653163 31706e6a 76527455 7578624b 5a376552 76754764 51417839 4d676e48 3434726b 50477669 4d617a71 45656e57 467a7573 6b49444d 75647276 6e723767 56685678 55495135 6b376e66 6c4f476a 6d566455 6163306c 71322b79 586b656a 77367a70 64784b73 554f7057 63736a48 436f6b36 73543941 44563676 4c645438 48366248 34572f74 6653722b 5735324b 4759766a 61343648 41786c54 6e73632b 6c645034 4b317553 38384d53 53586273 3732525a 57593873 79675a42 2b754f50 776f7031 337a7546 52576472 2f493944 45356268 33686672 65437163 3855374f 36733964 6e364856 30566936 44346e73 7645526e 46704863 49594e70 627a6c41 7a6e5054 42507055 57742b4c 74503047 2b6a744c 714b3565 52347849 504b5653 4143534f 35486f61 31397244 6c557236 4d34466c 2b4b6466 36767950 6e376454 666f6f6f 72513477 6f6f6f6f 414b4b4b 4b414369 69696741 6f6f6f6f 4138702b 4d2b7136 42396774 394a764c 4f533731 6478356c 71595732 74426b34 79546735 42492b37 6a6e4862 67313574 712f6772 786c7048 68693175 3952676e 66536f6a 35707454 4f574676 6b395751 48356335 366a706e 6e427272 2f697a34 64317130 38585165 4b624331 6b75725a 4652324b 526c7843 3066507a 67644634 7a6e7031 71747133 786e7674 66305766 52725477 2b715856 3748396e 33724f5a 636c766c 4f314e6f 4a4a7a78 79656364 616d4e2b 56323375 5539306e 73656865 4166454f 6b613734 4a615053 724e6241 57635a69 6b74464f 52476345 3542366b 486b3550 4a4f6331 7a666748 534c4c56 64576d61 39694571 77494857 4e76756b 35376a76 394b7466 436a776c 71486833 77337164 35716354 3238392b 6f4b774f 4 d4d694b 72594c44 73547550 42365971 54345944 2f695a58 35376553 76383635 36365573 58432b75 6e364d38 7a465255 716c4a50 61372f51 6f65504e 4c744c44 7844436c 70436b4b 54784b7a 4967776f 4f534f42 32365663 38576544 6a70634d 4e2f7061 66364e44 47424d43 666d4441 2f664f65 75636a36 59394f69 2f45662f 414a474f 782f3634 722f3645 61327648 6d72586c 6c5a477a 5730456c 6c645162 476d7752 73665048 50546f4f 6c635534 51396e55 62585632 2f543548 72635079 72307331 6d384a46 4f64746e 6f6e6458 6136626e 47366466 61326644 576f5764 6a417639 6e676d53 346c7839 30454146 51536365 6e413572 6f2f4171 6e2f6845 39616248 42336748 2f746e58 4c61564a 71393570 6b2b6936 5a62764a 48504b72 7a4d6938 2b674250 52527833 394b3955 30585155 30727734 4e4d4c41 76496a65 63343773 77776365 33623643 716f516c 55764a58 66753231 37396b66 56346d72 39557933 3246616c 476c4f55 6b2b534c 765a4a33 75374e2f 49354c34 58663637 552f3841 646a2f39 6d716a38 53503841 6b614c66 2f723154 2f774244 6571656b 61706665 42395775 6f627179 4c6c3132 74475732 5a775468 6c4f446b 64 61722b 4a357456 76745168 3148564c 5532776e 42574349 6a425646 50544858 71657036 35394d56 4b714a30 59302b71 65763373 39716e68 4a764f66 726c3179 536a6f37 72585262 4c667066 30506154 314e4a53 6e716153 7658507a 554b4b4b 4b414369 69696741 6f6f6f6f 414b4b4b 4b414369 69696741 6f6f6f6f 414b4b4b 4b414369 69696741 6f6f6f6f 414b4b4b 4b414369 69696741 6f6f6f6f 414b4b4b 4b414369 69696741 6f6f6f6f 414b4b4b 4b414369 69696741 6f6f6f6f 414b4b4b 4b414369 69696741 6f6f6f6f 414b4b4b 4b414369 69696744 2f396b3d>

  3. 第三个NSLog给了我image width:0.000000

  4. 第四个NSLog给了我img :- (null)
  5. 我做错了什么。

    我在键值对中完美地获得了JSON。 我想仅以NSData格式将NSString添加到UIImage。 我很好地获得了JSON和NSData, 但我的UIImage没有采用NSData。

    任何人都可以指导我。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

根据您提供的base64字符串,它是有效的,我已经检查过..它返回了验证码图像,您也可以通过以下链接进行验证: - http://www.askapache.com/online-tools/base64-image-converter/

这是一个非常简单的方法: -

//First Convert your Base 64 NSString to NSData
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];

//Then display it
 self.imgView.image = [UIImage imageWithData:data];