UIImage
をBase64文字列に変換してから元に戻す方法を知っている人はいますか?
私は以下のコードを持っています。エンコード前の元の画像は問題ありませんが、エンコードしてデコードした後に空白の画像が表示されるだけです。
NSData *imageData = UIImagePNGRepresentation(viewImage);
NSString *b64EncStr = [self encode: imageData];
NSString *base64String = [self encodeBase64:imageData];
最初に画像のNSDataが必要です
//Use image name from bundle to create NSData
let image : UIImage = UIImage(named:"imageNameHere")!
//Now use image to create into NSData format
let imageData:NSData = UIImagePNGRepresentation(image)!
//OR next possibility
//Use image's path to create NSData
let url:NSURL = NSURL(string : "urlHere")!
//Now use image to create into NSData format
let imageData:NSData = NSData.init(contentsOfURL: url)!
Swift 2.0>エンコード
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
Swift 2.0>デコード
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
Swift 3.0>デコード
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
エンコーディング:
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
print(strBase64)
デコード:
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
print(decodedimage)
yourImageView.image = decodedimage
Swift 3.0
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
let decodedimage = UIImage(data: dataDecoded)
yourImageView.image = decodedimage
NSData's
base64EncodedStringWithOptions
を使うことができます
エンコーディング:
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
デコード:
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
最初のオプション: this を使用してencode
およびdecode
imageにリンクします
project
に Base64 classを追加します。
エンコーディング:
NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
NSString *strEncoded = [Base64 encode:data];
デコード:
NSData* data = [Base64 decode:strEncoded ];;
image.image = [UIImage imageWithData:data];
他のオプション:encoding
とdecoding
には QSUtilities を使います。
Swift 4.2の拡張方法
extension UIImage {
func toBase64() -> String? {
guard let imageData = self.pngData() else { return nil }
return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
}
}
XCode 9.1とSwift 4.0
//
// Convert UIImage to a base64 representation
//
class func convertImageToBase64(image: UIImage) -> String {
let imageData = UIImagePNGRepresentation(image)!
return imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
}
//
// Convert a base64 representation to a UIImage
//
class func convertBase64ToImage(imageString: String) -> UIImage {
let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
return UIImage(data: imageData)!
}
Swift 3.0、XCODE 8.0
StringをあなたのURLに置き換えてください。 testImageはImageViewのアウトレットです
// Put Your Image URL
let url:NSURL = NSURL(string : "http://.jpg")!
// It Will turn Into Data
let imageData : NSData = NSData.init(contentsOf: url as URL)!
// Data Will Encode into Base64
let str64 = imageData.base64EncodedData(options: .lineLength64Characters)
// Now Base64 will Decode Here
let data: NSData = NSData(base64Encoded: str64 , options: .ignoreUnknownCharacters)!
// turn Decoded String into Data
let dataImage = UIImage(data: data as Data)
// pass the data image to image View.:)
testImage.image = dataImage
お役に立てば幸いです。
スイフトiOS8
// prgm mark ----
// convert images into base64 and keep them into string
func convertImageToBase64(image: UIImage) -> String {
var imageData = UIImagePNGRepresentation(image)
let base64String = imageData.base64EncodedStringWithOptions(.allZeros)
return base64String
}// end convertImageToBase64
// prgm mark ----
// convert images into base64 and keep them into string
func convertBase64ToImage(base64String: String) -> UIImage {
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0) )
var decodedimage = UIImage(data: decodedData!)
return decodedimage!
}// end convertBase64ToImage
Swift 3.0
画像をbase64文字列に変換する
遊び場でテスト済み
var logo = UIImage(named: "image_logo")
let imageData:Data = UIImagePNGRepresentation(logo)
let base64String = imageData.base64EncodedString()
print(base64String)
@implementation UIImage (Extended)
- (NSString *)base64String {
NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
return [NSString stringWithUTF8String:[data bytes]];
}
@end
Swift 2.0ではこの拡張子を使ってください(Jonas Franzの功績による)
extension UIImage{
func toBase64() -> String{
let imageData = UIImagePNGRepresentation(self)!
return imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
}
}
let userImage:UIImage = UIImage(named: "Your-Image_name")!
let imageData:NSData = UIImagePNGRepresentation(userImage)! as NSData
let dataImage = imageData.base64EncodedString(options: .lineLength64Characters)
let imageData = dataImage
let dataDecode:NSData = NSData(base64Encoded: imageData!, options:.ignoreUnknownCharacters)!
let avatarImage:UIImage = UIImage(data: dataDecode as Data)!
yourImageView.image = avatarImage
エンコーディング
func ConvertImageToBase64String (img: UIImage) -> String {
return img.jpegData(compressionQuality: 1)?.base64EncodedString() ?? ""
}
デコード
func ConvertBase64StringToImage (imageBase64String:String) -> UIImage {
let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
let image = UIImage(data: imageData!)
return image!
}
注:xcode 10.2でテスト済み
エンコーディング
func ConvertImageToBase64String (img: UIImage) -> String {
let imageData:NSData = UIImageJPEGRepresentation(img, 0.50)! as NSData //UIImagePNGRepresentation(img)
let imgString = imageData.base64EncodedString(options: .init(rawValue: 0))
return imgString
}
デコード
func ConvertBase64StringToImage (imageBase64String:String) -> UIImage {
let imageData = Data.init(base64Encoded: imageBase64String, options: .init(rawValue: 0))
let image = UIImage(data: imageData!)
return image
}
注:xcode 9.4.1でテスト済み
スイフトエクステンション
extension UIImage{
func toBase64() -> String{
var imageData = UIImagePNGRepresentation(self)
return imageData.base64EncodedStringWithOptions(.allZeros)
}
}
Swift 4
enum ImageFormat {
case png
case jpeg(CGFloat)
}
extension UIImage {
func base64(format: ImageFormat) -> String? {
var imageData: Data?
switch format {
case .png: imageData = UIImagePNGRepresentation(self)
case .jpeg(let compression): imageData = UIImageJPEGRepresentation(self, compression)
}
return imageData?.base64EncodedString()
}
}
extension String {
func imageFromBase64() -> UIImage? {
guard let data = Data(base64Encoded: self) else { return nil }
return UIImage(data: data)
}
}
See my class - AppExtension.Swift
// MARK: - UIImage (Base64 Encoding)
public enum ImageFormat {
case PNG
case JPEG(CGFloat)
}
extension UIImage {
public func base64(format: ImageFormat) -> String {
var imageData: NSData
switch format {
case .PNG: imageData = UIImagePNGRepresentation(self)
case .JPEG(let compression): imageData = UIImageJPEGRepresentation(self, compression)
}
return imageData.base64EncodedStringWithOptions(.allZeros)
}
}
Swift版 - 画像のbase64を作成します
私の意見ではUIImagePNGRepresenatation()
の場合のImplicitly Unwrapped Optionalは安全ではないので、以下のような拡張子を使用することをお勧めします。
extension UIImage {
func toBase64() -> String? {
let imageData = UIImagePNGRepresentation(self)
return imageData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
}
}
スイフト4.2 | Xcode 10
extension UIImage {
/// EZSE: Returns base64 string
public var base64: String {
return self.jpegData(compressionQuality: 1.0)!.base64EncodedString()
}
}
Swift 3.0では
func decodeBase64(toImage strEncodeData: String) -> UIImage {
let dataDecoded = NSData(base64Encoded: strEncodeData, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!
let image = UIImage(data: dataDecoded as Data)
return image!
}
私はすべての解決策を試してみましたが、(Swift 4を使用して)私には解決できないものがあります。
let temp = base64String.components(separatedBy: ",")
let dataDecoded : Data = Data(base64Encoded: temp[1], options:
.ignoreUnknownCharacters)!
let decodedimage = UIImage(data: dataDecoded)
yourImage.image = decodedimage
IOS 7以降のObjective-Cでは、画像のURLから変換する方法は次のとおりです。
NSURL *url = [NSURL URLWithString:self.groove.thumbnailURL];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
NSString *base64String = [UIImagePNGRepresentation(image)
base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
Swift 5、Xcode 10。
let imageData = UIImage(named:"imagename").pngData()?.base64EncodedString(options: .lineLength64Characters)
print(imageData)
Swift 3.0とXcode 8.0
let imageData = UIImageJPEGRepresentation(imageView.image!, 1)
let base64String = (imageData! as Data).base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
print(base64String)