需要保存为.png,但我得到.jpg

时间:2011-07-13 21:27:34

标签: iphone png photo jpeg cgcontext

我正在保存到相册,我希望该文件是一个.png,但它保存为.jpeg?甚至可以将.png保存到相册吗?

这是我的代码:

CGContextRef MyCreateBitmapContext (int pixelsWide,int pixelsHigh)
{
    CGContextRef context = NULL;
    //CGColorSpaceRef colorSpace;
    void * bitmapData;
    int bitmapByteCount;
    int bitmapBytesPerRow;
    bitmapBytesPerRow = (pixelsWide * 4);
    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
    //colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapData = malloc( bitmapByteCount );

    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,//bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);

    if (context== NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );
    return context;
}

- (IBAction)save:(id)sender{

    myBitmapContext = MyCreateBitmapContext (400, 300);

    // ********** Your drawing code here **********

    CGContextSetRGBFillColor (myBitmapContext, 1, 0, 0, 1);
    CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 400, 300 ));


    // ********** Your drawing code here **********


    CGImageRef imageRef = CGBitmapContextCreateImage(myBitmapContext);
    UIImage * image = [[UIImage alloc] initWithCGImage:imageRef];

    //NSData* imdata = UIImagePNGRepresentation ( image );// get PNG representation

    //UIImage* im2 = [UIImage imageWithData:imdata]; // wrap UIImage around PNG representation

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); // save to photo album

    [image release];

    CGImageRelease(imageRef);
    CGContextRelease(myBitmapContext);
}

非常感谢任何想法

1 个答案:

答案 0 :(得分:3)

UIImage没有关联的文件类型。采用UIImage,提取PNG数据和构造新UIImage的代码只是浪费了CPU周期。生成的图像应与原始图像相同。 UIImageWriteToSavedPhotosAlbum()保存JPEG,因为它假设保存在相册中的照片是照片。而JPEG是照片的首选格式。

您可能需要提交bug report请求机制以将PNG保存到相册。

相关问题