首页 > 代码库 > iOS 字符串和图片互转

iOS 字符串和图片互转

    for (UIImage *myImg in _imgArray)

    {

        NSData *imageData = http://www.mamicode.com/UIImageJPEGRepresentation(myImg,0.5);

        NSString *_encodedImageStr = [imageData base64Encoding];

        [parameters setObject:_encodedImageStr forKey:[NSString stringWithFormat:@"img[%i]",imgCount]];

        imgCount ++;

    }

 

 

Base64字符串转UIImage图片:

NSData *_decodedImageData   = [[NSData alloc] initWithBase64Encoding:_encodedImageStr];

 

UIImage *_decodedImage      = [UIImage imageWithData:_decodedImageData];

 

NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));

 

 

UIImage图片转成base64字符串:

UIImage *_originImage = [UIImage imageNamed:@"full_playlist_hl.png"];

 

NSData *_data = http://www.mamicode.com/UIImageJPEGRepresentation(_image, 1.0f);

 

NSString *_encodedImageStr = [_data base64Encoding];

 

NSLog(@"===Encoded image:\n%@", _encodedImageStr);