UIImageWriteToSavedPhotosAlbum保存为PNG透明度? [英] UIImageWriteToSavedPhotosAlbum save as PNG with transparency?

查看:365
本文介绍了UIImageWriteToSavedPhotosAlbum保存为PNG透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UIImageWriteToSavedPhotosAlbum将UIImage保存到用户的相册中。问题是图像没有透明度并且是JPG。我已经正确地将像素数据设置为具有透明度,但似乎没有办法以透明度支持的格式进行保存。想法?

I'm using UIImageWriteToSavedPhotosAlbum to save a UIImage to the user's photo album. The problem is that the image doesn't have transparency and is a JPG. I've got the pixel data set correctly to have transparency, but there doesn't seem to be a way to save in a transparency-supported format. Ideas?

编辑:没有办法实现这一点,但是还有其他方法可以向用户提供PNG图像。其中之一是将图像保存在Documents目录中(详见下文)。完成后,您可以通过电子邮件发送,将其保存在数据库中等等。除非它是有损的非透明JPG,否则您无法将其放入相册(现在)。

There is no way to accomplish this, however there are other ways to deliver PNG images to the user. One of which is to save the image in the Documents directory (as detailed below). Once you've done that, you can email it, save it in a database, etc. You just can't get it into the photo album (for now) unless it is a lossy non-transparent JPG.

推荐答案

这是我之前注意到的问题,并在 Apple开发者论坛大约一年前。据我所知,它仍然是一个悬而未决的问题。

This is a problem I have noticed before and reported on the Apple Developer Forums about a year ago. As far as I know it is still an open issue.

如果您有时间,请花时间在 Apple Bug报告。如果有更多人报告此问题,Apple更有可能会修复此方法以输出无损,支持Alpha的PNG。

If you have a moment, please take the time to file a feature request at Apple Bug Report. If more people report this issue, it is more likely that Apple will fix this method to output non-lossy, alpha-capable PNG.

EDIT

如果您可以在内存中撰写图像,我认为以下内容可以起作用或者至少可以帮助您开始:

If you can compose your image in memory, I think something like the following would work or at least get you started:

- (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!");
}

这篇关于UIImageWriteToSavedPhotosAlbum保存为PNG透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆