UIimage的縮放執行緒實現安全和非執行緒安全操作

2021-06-03 02:07:05 字數 3037 閱讀 8382

關於縮放的執行緒安全和非執行緒安全操作.

非執行緒安全的操作只能在主線程中進行操作,對於大的處理肯定會消耗大量的時間,

如下面的方法

方法1使用uikit+

(uiimage

*)imagewithimage

uiimage

*)image scaledtosize

cgsize

)newsize;

cgcontextref bitmap;

if(sourceimage.imageorientation ==

uiimageorientationup

|| sourceimage.imageorientation ==

uiimageorientationdown

)else

if(sourceimage.imageorientation ==

uiimageorientationleft

)else

if(sourceimage.imageorientation ==

uiimageorientationright

)else

if(sourceimage.imageorientation ==

uiimageorientationup

)else

if(sourceimage.imageorientation ==

uiimageorientationdown)

cgcontextdrawimage

(bitmap,

cgrectmake(0

,0, targetwidth, targetheight), imageref);

cgimageref ref

= cgbitmapcontextcreateimage(

bitmap);

uiimage

* newimage =

[uiimage imagewithcgimage:

ref];

cgcontextrelease(

bitmap);

cgimagerelease(

ref);

return newimage; }

這種方法的好處是它是執行緒安全,加上它負責的 (使用正確的顏色空間和點陣圖資訊,處理影象方向)的小東西,uikit

版本不會。 如何

調整和保持長寬比

(如 aspectfill選項)?

它是非常類似於上述,方法,它看起來像這樣: +

(uiimage

*)imagewithimage

uiimage

*)sourceimagescaledtosizewithsameaspectratio

cgsize

)targetsize;

else

scaledwidth  

= width * scalefactor;

scaledheight = height* scalefactor;

// center the image

if(widthfactor > heightfactor)

else

if(widthfactor < heightfactor) }

cgimageref imageref=

[sourceimage cgimage];

cgbitmapinfo bitmapinfo

=cgimagegetbitmapinfo(

imageref);

cgcolorspaceref colorspaceinfo

=cgimagegetcolorspace(

imageref);

if(bitmapinfo == kcgimagealphanone)

cgcontextref bitmap;

if(sourceimage.imageorientation ==

uiimageorientationup

|| sourceimage.imageorientation ==

uiimageorientationdown

)else

// in the right or left cases, we need to switch scaledwidth andscaledheight,

// and also the thumbnail point

if(sourceimage.imageorientation ==

uiimageorientationleft

)else

if(sourceimage.imageorientation ==

uiimageorientationright

)else

if(sourceimage.imageorientation ==

uiimageorientationup

)else

if(sourceimage.imageorientation ==

uiimageorientationdown)

cgcontextdrawimage

(bitmap,

cgrectmake

(thumbnailpoint.x,thumbnailpoint.y, scaledwidth, scaledheight), imageref);

cgimageref ref

= cgbitmapcontextcreateimage(

bitmap);

uiimage

* newimage =

[uiimage imagewithcgimage:

ref];

cgcontextrelease(

bitmap);

cgimagerelease(

ref);

return newimage;

取消執行緒,是否會釋放執行緒的所有資源?

include include include include 取消執行緒,是否會釋放執行緒的所有資源?例子 void thread1 void arg int main int argc,char argv pthread cancel t1 printf much too importent這行...

Qt開發 釋放執行緒中建立的QUdpSocket物件

在做symbian應用開發和meego應用開發時q框架是乙個統一的開發框架,很多時候需要在qthread的run中new乙個qudpsocket來收發資料.這時,這個socket物件的釋放就成了乙個麻煩的問題.51cto編者按 在做symbian應用開發和meego應用開發時q框架是乙個統一的開發框...

ios ScrollView縮放的實現原理

我們先看一下uiscrollview的幾個 方法 返回被縮放的檢視 func viewforzooming in scrollview uiscrollview uiview?縮放完成之後執行一次,這裡view是被縮放的檢視 func scrollviewdidendzooming scrollvi...