English 中文(简体)
UIImageWrite ToSavedPhotos 文本不像有透明度的巴布亚新几内亚那样使用?
原标题:UIImageWriteToSavedPhotosAlbum save as PNG with transparency?
  • 时间:2009-09-28 20:41:36
  •  标签:

使用UIImageWrite ToSavedPhotosAlbum,为用户的照片保存一个UIImage。 问题在于,形象没有透明度,而且是一个联合媒体。 我将六氯三苯数据集正确到 have>/em>透明度上,但似乎没有办法以透明支持的形式加以挽救。 想法?

EDIT:没有办法做到这一点,但还有其他办法向用户提供PNG图像。 其中之一是在文件目录中保存图像(详见下文)。 一旦你这样做,你就能够进行电子邮件,将其保存在数据库中。 除非是失去透明度的公益物,否则你可以将它带入(现在)。

最佳回答

这是我在一年前在Apple Development Forums上注意到并报告的问题。 我知道,这仍然是一个开放的问题。

如果你有一段时间,请花时间在

http://www.ohchr.org。

如果你能够把自己的形象描绘成记忆,我想像以下东西会奏效,或者至少会开始:

- (UIImage *) composeImageWithWidth:(NSInteger)_width andHeight:(NSInteger)_height {
    CGSize _size = CGSizeMake(_width, _height);
    UIGraphicsBeginImageContext(_size);

    // Draw image with Quartz 2D routines over here...

    UIImage *_compositeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return _compositeImage;
}

//
// cf. https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html#//apple_ref/doc/uid/TP40007072-CH21-SW20
//

- (BOOL) writeApplicationData:(NSData *)data toFile:(NSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
        return NO;
    }
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    return ([data writeToFile:appFile atomically:YES]);
}

// ...

NSString *_imageName = @"myImageName.png";
NSData *_imageData = [NSData dataWithData:UIImagePNGRepresentation([self composeImageWithWidth:100 andHeight:100)];

if (![self writeApplicationData:_imageData toFile:_imageName]) {
    NSLog(@"Save failed!");
}
问题回答

快速5:

func pngFrom(image: UIImage) -> UIImage {
    let imageData = image.pngData()!
    let imagePng = UIImage(data: imageData)!
    return imagePng
}

我建立了UIImage的延伸,并进行了安全分类:

<>Extension

extension UIImage {
    func toPNG() -> UIImage? {
        guard let imageData = self.pngData() else {return nil}
        guard let imagePng = UIImage(data: imageData) else {return nil}
        return imagePng
    }
}

<>Usage:

let image = //your UIImage
if let pngImage = image.toPNG() {
     UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil)
}

替代为UIImageWrite ToSavedPhotos创建二级UIImage 巴布亚新几内亚的数据可以直接使用PHPhotoLibrary书写。

这里是称为“除去照相”的UIImage延伸,这样做是:

extension UIImage {

    func saveToPhotos(completion: @escaping (_ success:Bool) -> ()) {

        if let pngData = self.pngData() {

            PHPhotoLibrary.shared().performChanges({ () -> Void in

                let creationRequest = PHAssetCreationRequest.forAsset()
                let options = PHAssetResourceCreationOptions()

                creationRequest.addResource(with: PHAssetResourceType.photo, data: pngData, options: options)

            }, completionHandler: { (success, error) -> Void in

                if success == false {

                    if let errorString = error?.localizedDescription  {
                        print("Photo could not be saved: (errorString))")
                    }

                    completion(false)
                }
                else {
                    print("Photo saved!")

                    completion(true)
                }
            })
        }
        else {
            completion(false)
        }

    }
}

使用:

    if let image = UIImage(named: "Background.png") {
        image.saveToPhotos { (success) in
            if success {
                // image saved to photos
            }
            else {
                // image not saved
            }
        }
    }




相关问题