简体   繁体   English

从照片库加载图像

[英]Load image from photo library

  1. How can I load an image from photo library and show it in imageView ? 如何从照片库加载图像并将其显示在imageView中
  2. How can I change image to black and white ? 如何将图像更改为黑白

If you want to load an image from photo library, you have to use UIImagePickerController class. 如果要从照片库加载图像,则必须使用UIImagePickerController类。 Refer this Link 引用此链接

To convert image form UIImagePickerController to black and white you can use this code: 要将图像形式的UIImagePickerController转换为黑白,可以使用以下代码:

UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; // this image we get from UIImagePickerController

CGColorSpaceRef colorSapce = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, originalImage.size.width, originalImage.size.height, 8, originalImage.size.width, colorSapce, kCGImageAlphaNone);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, originalImage.size.width, originalImage.size.height), [originalImage CGImage]);

CGImageRef bwImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSapce);

UIImage *resultImage = [UIImage imageWithCGImage:bwImage]; // This is result B/W image.
CGImageRelease(bwImage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM