簡體   English   中英

iOS中的圖像循環包裝

[英]Image Circular Wrap in iOS

我有一個問題-我想創建一個循環包裝功能,該功能將包裝圖像,如下所示:

影像換行

這在OSX中可用,但在iOS中不可用。

到目前為止,我的邏輯是:

將圖像分成x部分,每個部分:

  1. 旋轉alpha角度
  2. 在x軸上縮放圖像,以創建圖像的菱形“扭曲”效果
  3. 向后旋轉90 - atan((h / 2) / (w / 2))
  4. 平移偏移

我的問題是,這似乎不准確,而且我無法從數學上弄清楚如何正確執行此操作-任何幫助將不勝感激。

鏈接到CICircularWrap OSX文檔:

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/filter/ci/CICircularWrap

由於CICircularWrap上不支持CICircularWrap (編輯:現在是-請在下面檢查答案) ,因此現在必須編寫自己的效果。 可能最簡單的方法是計算從極坐標到笛卡爾坐標系的轉換,然后從源圖像進行插值。 我提出了一個簡單的算法(坦率地說很慢-可以進行很多優化):

    #import <QuartzCore/QuartzCore.h>

    CGContextRef CreateARGBBitmapContext (size_t pixelsWide, size_t pixelsHigh)
    {
        CGContextRef    context = NULL;
        CGColorSpaceRef colorSpace;
        void *          bitmapData;
        int             bitmapByteCount;
        int             bitmapBytesPerRow;

        // Declare the number of bytes per row. Each pixel in the bitmap in this
        // example is represented by 4 bytes; 8 bits each of red, green, blue, and
        // alpha.
        bitmapBytesPerRow   = (int)(pixelsWide * 4);
        bitmapByteCount     = (int)(bitmapBytesPerRow * pixelsHigh);

        // Use the generic RGB color space.
        colorSpace = CGColorSpaceCreateDeviceRGB();
        if (colorSpace == NULL)
        {
            fprintf(stderr, "Error allocating color space\n");
            return NULL;
        }

        // Allocate memory for image data. This is the destination in memory
        // where any drawing to the bitmap context will be rendered.
        bitmapData = malloc( bitmapByteCount );
        if (bitmapData == NULL)
        {
            fprintf (stderr, "Memory not allocated!");
            CGColorSpaceRelease( colorSpace );
            return NULL;
        }

        // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
        // per component. Regardless of what the source image format is
        // (CMYK, Grayscale, and so on) it will be converted over to the format
        // specified here by CGBitmapContextCreate.
        context = CGBitmapContextCreate (bitmapData,
                                         pixelsWide,
                                         pixelsHigh,
                                         8,      // bits per component
                                         bitmapBytesPerRow,
                                         colorSpace,
                                         kCGImageAlphaPremultipliedFirst);
        if (context == NULL)
        {
            free (bitmapData);
            fprintf (stderr, "Context not created!");
        }

        // Make sure and release colorspace before returning
        CGColorSpaceRelease( colorSpace );

        return context;
    }

    CGImageRef circularWrap(CGImageRef inImage,CGFloat bottomRadius, CGFloat topRadius, CGFloat startAngle, BOOL clockWise, BOOL interpolate)
    {
        if(topRadius < 0 || bottomRadius < 0) return NULL;

        // Create the bitmap context
        int w = (int)CGImageGetWidth(inImage);
        int h = (int)CGImageGetHeight(inImage);

        //result image side size (always a square image)
        int resultSide = 2*MAX(topRadius, bottomRadius);
        CGContextRef cgctx1 = CreateARGBBitmapContext(w,h);
        CGContextRef cgctx2 = CreateARGBBitmapContext(resultSide,resultSide);

        if (cgctx1 == NULL || cgctx2 == NULL)
        {
            return NULL;
        }

        // Get image width, height. We'll use the entire image.
        CGRect rect = {{0,0},{w,h}};

        // Draw the image to the bitmap context. Once we draw, the memory
        // allocated for the context for rendering will then contain the
        // raw image data in the specified color space.
        CGContextDrawImage(cgctx1, rect, inImage);

        // Now we can get a pointer to the image data associated with the bitmap
        // context.
        int *data1 = CGBitmapContextGetData (cgctx1);
        int *data2 = CGBitmapContextGetData (cgctx2);

        int resultImageSize = resultSide*resultSide;
        double temp;
        for(int *p = data2, pos = 0;pos<resultImageSize;p++,pos++)
        {
            *p = 0;
            int x = pos%resultSide-resultSide/2;
            int y = -pos/resultSide+resultSide/2;
            CGFloat phi = modf(((atan2(x, y)+startAngle)/2.0/M_PI+0.5),&temp);
            if(!clockWise) phi = 1-phi;
            phi*=w;
            CGFloat r = ((sqrtf(x*x+y*y))-topRadius)*h/(bottomRadius-topRadius);
            if(phi>=0 && phi<w && r>=0 && r<h)
            {
                if(!interpolate || phi >= w-1 || r>=h-1)
                {
                    //pick the closest pixel
                    *p = data1[(int)r*w+(int)phi];
                }
                else
                {
                    double dphi = modf(phi, &temp);
                    double dr = modf(r, &temp);

                    int8_t* c00 = (int8_t*)(data1+(int)r*w+(int)phi);
                    int8_t* c01 = (int8_t*)(data1+(int)r*w+(int)phi+1);
                    int8_t* c10 = (int8_t*)(data1+(int)r*w+w+(int)phi);
                    int8_t* c11 = (int8_t*)(data1+(int)r*w+w+(int)phi+1);

                    //interpolate components separately
                    for(int component = 0; component < 4; component++)
                    {
                        double avg = ((*c00 & 0xFF)*(1-dphi)+(*c01 & 0xFF)*dphi)*(1-dr)+((*c10 & 0xFF)*(1-dphi)+(*c11 & 0xFF)*dphi)*dr;
                        *p += (((int)(avg))<<(component*8));
                        c00++; c10++; c01++; c11++;
                    }
                }
            }
        }

        CGImageRef result = CGBitmapContextCreateImage(cgctx2);

        // When finished, release the context
        CGContextRelease(cgctx1);
        CGContextRelease(cgctx2);
        // Free image data memory for the context
        if (data1) free(data1);
        if (data2) free(data2);

        return result;
    }

使用帶有參數的circularWrap函數:

  • CGImageRef inImage源圖像
  • CGFloat bottomRadius源圖像的底面將轉換為具有此半徑的圓
  • CGFloat topRadius與源圖像的頂部相同,它可以大於或小於底部半徑。 (結果是環繞圖像的頂部/底部)
  • CGFloat startAngle源圖像的左側和右側將轉換的角度。 BOOL clockWise渲染方向
  • BOOL interpolate簡單的抗鋸齒算法。 僅對圖像內部進行插值

一些樣本(左上方是源圖像):
一些樣本(左上方是源圖像) 使用代碼:

    image1 = [UIImage imageWithCGImage:circularWrap(sourceImage.CGImage,0,300,0,YES,NO)];
    image2 = [UIImage imageWithCGImage:circularWrap(sourceImage.CGImage,100,300,M_PI_2,NO,YES)];
    image3 = [UIImage imageWithCGImage:circularWrap(sourceImage.CGImage,300,200,M_PI_4,NO,YES)];
    image4 = [UIImage imageWithCGImage:circularWrap(sourceImage.CGImage,250,300,0,NO,NO)];

請享用! :)

蘋果已將CICircularWrap添加到iOS 9

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CICircularWrap

將圖像環繞在透明圓圈周圍。

本地化顯示名稱

圓形包裝紙變形

可用性

在OS X v10.5和更高版本以及在iOS 9和更高版本中可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM