我正在尝试制作缩略图并保存到文档目录中。 但问题是,当我试图将缩略图转换为NSData时。它返回nil。
这是我的代码,
UIImage *thumbNailimage=[image thumbnailImage:40 transparentBorder:0.2 cornerRadius:0.2 interpolationQuality:1.0];
NSData *thumbNailimageData = UIImagePNGRepresentation(thumbNailimage);// Returns nil
[thumbNailimageData writeToFile:[DOCUMENTPATH stringByAppendingPathComponent:@"1.png"] atomically:NO];
那么我用UIImageJPEGRepresentation尝试过的问题是什么,但它对我不起作用。
感谢。
答案 0 :(得分:16)
试试这个:
UIGraphicsBeginImageContext(originalImage.size);
[originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这会创建原始UIImage的副本。然后,您可以致电UIImagePNGRepresentation
,它将正常运作。
答案 1 :(得分:4)
对于Swift程序员来说,Rickster的答案对我帮助很大! UIImageJPEGRepresentation 在选择某个图片时崩溃了我的应用。我正在分享我的UIImage扩展(或Objective-C术语中的类别)。
Heap(){
root = NULL;
cout << "Do you want\n1.Max Heap\n2.Min Heap";
cin >> choice;
if(choice == 1){
function_variable = max_insert();
}else{
function_variable = min_insert();
}
cout << "1.INSERT\2.SEARCH\n3.TRAVERSAL";
cin >> choice;
switch(choice){
case1: function_variable ;break;
case2: search();break;
case3:traversal();break;
}
}
答案 2 :(得分:3)
试试这段代码,
-(void) createThumbnail
{
UIImage *originalImage = imgView2.image; // Give your original Image
CGSize destinationSize = CGSizeMake(25, 25); // Give your Desired thumbnail Size
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *thumbNailimageData = UIImagePNGRepresentation(newImage);
UIGraphicsEndImageContext();
[thumbNailimageData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"1.png"] atomically:NO];
}
希望这有助于你, 快乐编码