标签: cgimage

从ALAssetRepresentation生成自定义缩略图

我的主要问题是我需要获取ALAsset对象的缩略图.

我尝试了很多解决方案并搜索了堆栈溢出数天,由于这些限制,我找到的所有解决方案都不适合我:

  • 我无法使用默认缩略图,因为它太少了;
  • 我无法使用fullScreen或fullResolution图像,因为我在屏幕上有很多图像;
  • 我无法使用UIImage或UIImageView进行大小调整,因为它们会加载fullResolution图像
  • 我无法将图像加载到内存中,我正在处理20Mpx图像;
  • 我需要创建一个200x200像素版本的原始资产才能加载到屏幕上;

这是我带来的代码的最后一次迭代:

#import <AssetsLibrary/ALAsset.h>
#import <ImageIO/ImageIO.h>   

// ...

ALAsset *asset;

// ...

ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation];

NSDictionary *thumbnailOptions = [NSDictionary dictionaryWithObjectsAndKeys:
    (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
    (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
    (id)[NSNumber numberWithFloat:200], kCGImageSourceThumbnailMaxPixelSize,
    nil];

CGImageRef generatedThumbnail = [assetRepresentation CGImageWithOptions:thumbnailOptions];

UIImage *thumbnailImage = [UIImage imageWithCGImage:generatedThumbnail];
Run Code Online (Sandbox Code Playgroud)

问题是,结果CGImageRef既不是由方向变换的,也不是由指定的最大像素大小变换的;

我也试图找到一种调整大小的方法CGImageSource,但是:

  • 资产网址不能用于CGImageSourceCreateWithURL:;
  • 我不能从提取ALAssetALAssetRepresentation一个CGDataProviderRef与使用CGImageSourceCreateWithDataProvider:;
  • CGImageSourceCreateWithData: 要求我将fullResolution或全屏资产存储在内存中才能工作.

我错过了什么吗?

有没有从获得自定义缩略图的另一种方式ALAsset或者ALAssetRepresentation说我失踪?

提前致谢.

thumbnails cgimage ios alasset alassetslibrary

13
推荐指数
1
解决办法
6470
查看次数

Swift 3.0:如何调用CGImageCreateWithImageInRect()?

我需要在Swift 3.0中实现这个Objective-C代码(我正在使用Xcode 8 Beta 3):

// Note: this code comes from an Obj-C category on UIImage
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, cropRect);
UIImage *image = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
Run Code Online (Sandbox Code Playgroud)

我在最新的文档中找不到任何内容CGImageCreateWithImageInRect().

cgimage swift swift3 xcode8-beta3

13
推荐指数
1
解决办法
8257
查看次数

在UI线程上懒洋洋地加载CGImage/UIImage会导致口吃

我的程序显示一个水平滚动表面,从左到右平铺UIImageViews.代码在UI线程上运行,以确保新显示的UIImageViews分配了一个新加载的UIImage.加载发生在后台线程上.

一切都很好,除了每个图像变得可见时都有口吃.起初我以为我的后台工作者在UI线程中锁定了一些内容.我花了很多时间查看它,并最终意识到UIImage在UI线程第一次变得可见时正在做一些额外的延迟处理.这让我很困惑,因为我的工作线程有明确的解压缩JPEG数据的代码.

无论如何,在预感上,我写了一些代码,以渲染到后台线程上的临时图形上下文中 - 当然,口吃也消失了.UIImage现在正在我的工作线程上预先加载.到现在为止还挺好.

问题是我的新"强力懒惰的图像"方法是不可靠的.它会导致间歇性的EXC_BAD_ACCESS.我不知道UIImage在幕后实际上在做什么.也许它正在解压缩JPEG数据.无论如何,方法是:

+ (void)forceLazyLoadOfImage: (UIImage*)image
{
 CGImageRef imgRef = image.CGImage;

 CGFloat currentWidth = CGImageGetWidth(imgRef);
 CGFloat currentHeight = CGImageGetHeight(imgRef);

    CGRect bounds = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

 CGAffineTransform transform = CGAffineTransformIdentity;
 CGFloat scaleRatioX = bounds.size.width / currentWidth;
 CGFloat scaleRatioY = bounds.size.height / currentHeight;

 UIGraphicsBeginImageContext(bounds.size);

 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextScaleCTM(context, scaleRatioX, -scaleRatioY);
 CGContextTranslateCTM(context, 0, -currentHeight);
 CGContextConcatCTM(context, transform);
 CGContextDrawImage(context, CGRectMake(0, 0, currentWidth, currentHeight), imgRef);

 UIGraphicsEndImageContext();
}
Run Code Online (Sandbox Code Playgroud)

并且EXC_BAD_ACCESS发生在CGContextDrawImage行上.问题1:我是否允许在UI线程以外的线程上执行此操作?问题2:什么是UIImage实际上"预加载"?问题3:解决这个问题的官方方法是什么?

感谢阅读所有这些,任何建议将不胜感激!

iphone lazy-loading exc-bad-access uiimage cgimage

12
推荐指数
3
解决办法
1万
查看次数

为什么我的图像的每行字节数超过其每像素的字节数乘以其宽度?

我正在将图像转换为MOV文件,并且有一件有趣的事情发生在我身上.我记录了每个颜色分量的位数,每个像素的位数和每行的字节数.这是我的代码:

NSLog(@"Image width: %d, Height: %d", CGImageGetWidth(image), CGImageGetHeight(image));
NSLog(@"BPC: %d \ BPP: %d \ ByPR: %d", CGImageGetBitsPerComponent(image), CGImageGetBitsPerPixel(image), CGImageGetBytesPerRow(image));
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

图像宽度:300,高度:300(一切都如预期的那样)BPC:8(每种颜色8位......到目前为止很好)BPP:32(32 = 4个组件ARGB*每个颜色8位......得到它)ByPR :1216(每行300 px*每像素4个字节=每行1200个字节)

为什么我每行记录1216个字节,而不是1200?顺便说一句,这不仅仅是一个侥幸.当我根据缓冲区大小的数字创建视频时,它可以工作.当我用每行1200字节创建它时,我得到一些混乱的混叠效果.

思考?

video uiimage cgimage ios

12
推荐指数
1
解决办法
2736
查看次数

iOS 7中的图像解压缩

Stack Overflow已经讨论了图像解压缩的问题,但直到这个问题还有0个提及kCGImageSourceShouldCacheImmediately,iOS 7中引入的一个选项理论上解决了这个问题.从标题:

指定图像解码和缓存是否应在图像创建时发生.

Objc.io#7中, Peter Steinberger提出了这种方法:

+ (UIImage *)decompressedImageWithData:(NSData *)data 
{
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, (__bridge CFDictionaryRef)@{(id)kCGImageSourceShouldCacheImmediately: @YES});

    UIImage *image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    CFRelease(source);
    return image;
}
Run Code Online (Sandbox Code Playgroud)

像AFNetworking和SDWebImage这样的库仍然使用该CGContextDrawImage方法进行图像解压缩.来自SDWebImage:

+ (UIImage *)decodedImageWithImage:(UIImage *)image {
    if (image.images) {
        // Do not decode animated images
        return image;
    }

    CGImageRef imageRef = image.CGImage;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
    CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};

    CGColorSpaceRef …
Run Code Online (Sandbox Code Playgroud)

compression uiimage cgimage ios ios7

12
推荐指数
1
解决办法
2358
查看次数

为什么cgImage?.cropping旋转图像?

我写了一个UICropperViewController,它在横向模式下完美地适用于图像.纵向模式下的图像存在很大问题.下图显示了带有黄色裁剪框的简单图片:

在此输入图像描述

裁剪结果是:

在此输入图像描述

现在谈到肖像图像我们得到了这种情况:

在此输入图像描述

结果如下:

在此输入图像描述

那么这里发生了什么?原始图像自动向左旋转.

我研究了很多,基本上找到了两个建议:

建议1

在裁剪之前保存图像方向并恢复它.

func didTapCropButton(sender: AnyObject) {
    let originalOrientation = self.imageView.image?.imageOrientation;

    // raw value of originalOrientation is `3` so its rotated to the right

    let croppedCGImage = self.imageView.image?.cgImage?.cropping(to: self.cropArea);

    // create a cropped cgImage

    let croppedImage = UIImage(cgImage: croppedCGImage!, scale: (self.imageView.image?.scale)!, orientation: (originalOrientation)!);

    // create the UIImage with the result from cgImage cropping and original orientation

    if (self.callback != nil) {
        self.callback.croppingDone(image: croppedImage);
    }

    self.dismiss(animated: true, completion: nil);
}
Run Code Online (Sandbox Code Playgroud)

但结果现在是:

在此输入图像描述

显然这个建议不起作用,因为它只是简单地旋转已经裁剪的图像.

建议2

定位修复.我找到了以下代码,并承诺会修复错误: …

uiimageview uiimage cgimage ios swift

12
推荐指数
1
解决办法
4044
查看次数

在Swift中绘制图像需要永远

在我的应用程序中,我正在创建一个图像映射浮动到像素值并将其用作Google地图上的叠加层,但它需要永远做,Android中的同样事情几乎是即时的.我的代码看起来像这样:

private func imageFromPixels(pixels: [PixelData], width: Int, height: Int) -> UIImage? {

    let bitsPerComponent = 8
    let bitsPerPixel = bitsPerComponent * 4
    let bytesPerRow = bitsPerPixel * width / 8

    let providerRef = CGDataProvider(
        data: NSData(bytes: pixels, length: height * width * 4) 
    )

    let cgimage = CGImage(
        width: width,
        height: height,
        bitsPerComponent: bitsPerComponent,
        bitsPerPixel: bitsPerPixel,
        bytesPerRow: bytesPerRow,
        space: CGColorSpaceCreateDeviceRGB(),
        bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue),
        provider: providerRef!,
        decode: nil,
        shouldInterpolate: true,
        intent: .defaultIntent
    )

    if cgimage == nil {
        print("CGImage is not …
Run Code Online (Sandbox Code Playgroud)

google-maps cgimage ios swift

12
推荐指数
1
解决办法
415
查看次数

如何创建空/空UIImage或CGImage并操纵其上的像素(Xcode)?

我有以下问题,只能部分解决它.我正在使用XCode 3.2.5,为iOS 4+开发.

我只是尝试创建一个预定义大小的图像,操纵它上面的像素,然后在UIImageView中显示图像.我的观点包括连接到的简单Button和名为"buttonClicked"的IBAction以及名为"qrImage"的UIImageView,并设置为"IBOutlet".我使用以下代码:

头文件:myClassViewController.h

#import <UIKit/UIKit.h>

@interface myClassViewController : UIViewController
{
    IBOutlet UIImageView *qrImage;
}

@property (nonatomic, retain) UIImageView   *qrImage;

- (IBAction) buttonClicked;
- (UIImage*) drawQRImage;

@end
Run Code Online (Sandbox Code Playgroud)

Main-File myClassViewController.m

#import "myClassViewController.h"
#include <math.h>

@implementation myClassViewController
@synthesize qrImage;


-(UIImage*) drawQRImage
{
    // load Image
    //
    UIImage *image              = [UIImage imageNamed:@"blank.png"];    
    CGImageRef imageRef         = image.CGImage;
    NSData *data                = (NSData *) CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
    char *pixels                = (char *)[data bytes];

    unsigned long int startPixel, pixelNr;


    // manipulate the individual pixels
    //
    size_t width                = …
Run Code Online (Sandbox Code Playgroud)

iphone xcode pixel uiimage cgimage

11
推荐指数
1
解决办法
2万
查看次数

如果CGImageCreate的数据提供者使用应用程序创建的数组,那么对CGImageCreate的正确调用是什么样的呢?

我正在尝试在内存中创建一个位图作为模式函数的一部分,drawLayer:inContext:方法(此方法是CALayer委托协议的一部分)将调用.模式函数看起来类似于:

static const size_t kComponentsPerPixel = 4;
static const size_t kBitsPerComponent = sizeof(unsigned char) * 8;

NSInteger layerHeight = 160;
NSInteger layerWidth = 160;
CGContextSaveGState(context); 

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();

size_t bufferLength = layerWidth * layerHeight * kComponentsPerPixel;

unsigned char *buffer = malloc(bufferLength);

// The real function does something more interesting with the buffer, but I cut it 
// to reduce the complexity while I figure out the crash.
for (NSInteger i = 0; i < bufferLength; ++i)
{
    buffer[i] = …
Run Code Online (Sandbox Code Playgroud)

buffer objective-c cgimage rgba ios

11
推荐指数
1
解决办法
6064
查看次数

CGBitmapContextCreateImage - vm_copy失败 - iPhone SDK

我在我的iPhone应用程序中使用CGBitmapContextCreateImage时遇到问题.

我正在使用AV Foundation Framework使用此方法抓取相机帧:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);

    UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
    self.imageView.image = image;

    CGImageRelease(newImage);

} 
Run Code Online (Sandbox Code Playgroud)

但是,我在调试控制台中看到一个错误:

<Error>: CGDataProviderCreateWithCopyOfData: vm_copy failed: status 2.
Run Code Online (Sandbox Code Playgroud)

有没有人见过这个?通过注释掉我的问题,我将问题排成一行:

CGImageRef newImage = CGBitmapContextCreateImage(newContext);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何摆脱它.从功能上讲,它很棒.很明显,CGImage正在创建,但我需要知道是什么导致错误,所以它不会影响其他部分. …

iphone objective-c cgimage

10
推荐指数
1
解决办法
4824
查看次数