我正在保存到相冊,我希望該文件是.png,但它保存爲.jpeg文件?甚至可以將.png保存到相冊中?需要另存爲.png,但是我得到.jpg
這裏是我的代碼:
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);
}
任何想法
'UIImageWriteToSavedPhotosAlbum'獲取圖像數據並將jpg寫入相冊。我相信它並不關心底層圖像文件是什麼。作爲黑暗中的刺,請嘗試使用「UISaveVideoAtPathToSavedPhotosAlbum」。 – amattn
謝謝amattn,我會在稍後看看。 – mtompson